Muy bien entonces a usar libreria curl!.
Ahora he mejorado la funcion cambia imagen a resolucion de pantalla del usuario
version 2.0 by SurKaiser :
- Mejora notable en la seguridad :
*Se añadio base64_encode();
*Se añadio utf8_encode();
* Comprobacion de variables $_GET;
* Cerrados oyos de seguridad, ahora es mas seguro usarlo dentro de CMS.
- Funcion usar 'http://' o simplemente direccion del fichero:
*Se implemento funcion curl(); para el uso de http
*Se habilito opcion para usar http o solo fichero.
- Fixs/bugs reparados :
*Carga de imagen 50% mas rapida.
*Ahora es funcionable la carga de imagenes por default
Bueno esta funcion puede servirles a mas de uno es por eso que tambien la mejore de esta forma .
Necesitamos 2 archivos resolucion.html y comprueba.php
Para que funcione se deben editar :
en el archivo resolucion.html
solo 2 variables , la variable url es la que contendra la direccion con las imagenes y la variable usarHttp='' a true si utilizaremos http recuerda tener activada la libreria curl en tu servidor esto tambien habilitara imagenes externas , quiere decir que pueden ser de tu propio servidor o uno externo, a false en el caso de no contar con la libreria curl o en el caso de querer solo usar imagenes internas si lo dejas en false recuerda que en url no podras ingresar 'http://'
En el archivo comprueba.php editar las variables $url y $url en las lineas 42 y 68, estas estan vacias, aqui ingresaras la url de la imagen a mostrar en caso de que no exista la del usuario visitante.
Esto es todo aqui el codigo fuente :
resolucion.html :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/*
^ SurKaiser
^ Funcion, llama imagen de resolucion de pantalla
* Version 2.0
^ http://www.comocreartuweb.com
*
*
*/
var ancho = screen.width;
var alto = screen.height;
//Recuerda si usaras http:// , debes tener activada la funcion curl(); en php
//En caso contrario ingresa direccion interna del servidor (sin http://) y
//Modificas la variable usarHttp = 'true' a usarHttp = 'false';
var url = "DIRECCION DE LAS IMAGENES";
// ¿Usaremos http?
//Si usaremos http:// entonces usarHttp = 'true' si no usaremos http, entonces usarHttp='false'
var usarHttp = '';
//Extension de la imagen a usar, cambia la extension de imagen por la que desees usar(por defecto .jpg)
var extension = ".jpg";
//Necesitamos mas seguridad xD
var Base64 = {
_llaveStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encryptar : function (entrada) {
var salida = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
entrada = Base64._utf8_encryptar(entrada);
while (i < entrada.length) {
chr1 = entrada.charCodeAt(i++);
chr2 = entrada.charCodeAt(i++);
chr3 = entrada.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
salida = salida +
this._llaveStr.charAt(enc1) + this._llaveStr.charAt(enc2) +
this._llaveStr.charAt(enc3) + this._llaveStr.charAt(enc4);
}
return salida;
},
_utf8_encryptar : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
};
var nuevaUrl = url + ancho + "x" + alto + extension;
usarHttp = Base64.encryptar(usarHttp);
nuevaUrl = Base64.encryptar(nuevaUrl);
//Ahora necesitamos comprobar que la imagen de su resolucion exista
//Asi que tendremos que llamar a php para que compruebe algunas cosillas
//Para eso usaremos un poco de ajax =)
function obtenerXMLHTTP()
{
try{
obtener = new XMLHttpRequest();
} catch(err1) {
try {
obtener = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(err2) {
try {
obtener = new ActiveXObject("Microsoft.msxml2");
}
catch(err3) {
obtener = false;
}
}
}
return obtener;
}
ajax = new obtenerXMLHTTP();
function archivoExiste()
{
var archivo = "comprueba.php?usar=" + usarHttp + "&url="+ nuevaUrl;
ajax.open('GET',archivo, true);
ajax.onreadystatechange = respuesta;
ajax.send(null);
}
function respuesta()
{
if(ajax.readyState == 4)
{
if(ajax.status == 200)
{
var miImagen = document.getElementById("imagen");
miImagen.src = ajax.responseText;
//En caso de que carge la imagen por default, aseguremonos ajustandola
miImagen.widht = ancho;
miImagen.height= alto;
}
}
}
archivoExiste();
</script>
<title>Cambia Imagen A resolucion By SurKaiser</title>
</head>
<body>
<img src="" id="imagen" />
</body>
</html>
comprueba.php :
<?php
/*
^ SurKaiser
^ Funcion, llama imagen de resolucion de pantalla
* Version 2.0
^ http://www.comocreartuweb.com
*
*
*/
//Seguridad xD
$http = $_GET['usar'];
$http = htmlspecialchars($http);
$http = htmlspecialchars($http, ENT_QUOTES);
$http = base64_decode($http);
$http = utf8_decode($http);
//Repetimos con la url xD
$url = $_GET['url'];
$url = htmlspecialchars($url);
$url = htmlspecialchars($url, ENT_QUOTES);
$url = base64_decode($url);
$url = utf8_decode($url);
if($http == '')
{
}
else if($http == 'true')
{
//Si usaremos http, entonces usaremos curl
//Recuerda tenerla activada en php
$fp = curl_init($url);
$abrir = curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
$abrir = curl_setopt($fp, CURLOPT_TIMEOUT, 30);
$abir = curl_exec($fp);
$info = curl_getinfo($fp, CURLINFO_HTTP_CODE);
curl_close($fp);
switch($url)
{
case '' :
break;
default:
if($info == 404){
//No existe la imagen , entonces mostraremos la imagen por default, o la que quieras mostrar
$url= "";
echo $url;
exit;
}else{
//Existio la imagen mostremosla xD
echo $url;
exit;
}
break;
}
} else if($http == 'false')
{
//Si no usaremos http:// , entonces comprobemos si existe en el servidor
if(file_exists($url))
{
//Si el archivo existe le mostramos la url del archivo
echo $url;
}
else {
//Si el archivo de su resolucion no existe
//Mostramos una imagen por default, que automaticamente
//Sera ajustada al tamaño de su resolucion
//Recomiendo una de un tamaño grande
//por si se modifica no se deforme
//Tambien tener imagenes de las resoluciones mas comunes
//Como 800x600 , 1280x1024 etc.
$url = "";
echo $url;
}
} else {
echo " ";
}
?>
Saludos! [;)]