Buenas!
Hace tiempo cree un script para mostrar imagenes redimensionadas sin tener que guardarla por segunda vez... La verdad es que es muy util y funciona genial.
Lo que hace este pequeño script es coger la imagen y redimensionarla al ancho que le pongas en la variable
w y luego la ajusta proporcionalmente según la imagen original. También se puede añadir otra variable llamada
h para especificar un alto
(este campo es opcional, y de no ponerlo, y tampoco poner un height="" en el img se realizará la proporción con el ancho especificado en w)... Al poner los dos parametros, el resultado será una imagen proporcional con el tamaño especificado de la original, y en caso de que no coincidan las dos proporciones, se recortará el centro de dicha proporción.
Así explicado suena un poco lio, pero pruebalo :)
El miniscript funcionaría asi:
Código HTML:
<img src="img.php?imagen=rutadelaimagen&w=60&h=60" width="60" height="60" border="0" alt="" />
Y este es el código:
Código PHP:
// IMG redimensionador (Héctor S. > [email protected])
//////////////////////////////////////////////////////////////////////////
if(file_exists($_GET["imagen"])){
$fotoperf = $_GET["imagen"];
header("Content-type: image/jpeg");
$fileextsp = split("/",$fotoperf);
$count = count($fileextsp) - 1;
$nomarch = $fileextsp[$count];
$fileext2 = pathinfo($nomarch);
$formatperf = strtolower($fileext2["extension"]);
$getsizesperf = getimagesize($fotoperf);
$widthf = $_GET["w"];
if($_GET["h"] == ""){
$h2 = $widthf * $getsizesperf[1];
$h2 = $h2 / $getsizesperf[0];
$heightf = $h2;
}else{
$heightf = $_GET["h"];
}
if($formatperf == "jpg" || $formatperf == "jpeg"){ $imgperf = imagecreatefromjpeg($fotoperf); }
if($formatperf == "gif"){ $imgperf = imagecreatefromgif($fotoperf); }
if($formatperf == "png"){ $imgperf = imagecreatefrompng($fotoperf); }
if($formatperf == "bmp"){ $imgperf = imagecreatefrombmp($fotoperf); }
if($getsizesperf[1] > $getsizesperf[0]){
$newheightperf = $widthf * $getsizesperf[1];
$newheightperf = $newheightperf / $getsizesperf[0];
$newwidthperf = $widthf;
if($newheightperf < $heightf){
$newwidthperf = $heightf * $getsizesperf[0];
$newwidthperf = $newwidthperf / $getsizesperf[1];
$newheightperf = $heightf;
}
}else{
$newwidthperf = $heightf * $getsizesperf[0];
$newwidthperf = $newwidthperf / $getsizesperf[1];
$newheightperf = $heightf;
if($newwidthperf < $widthf){
$newheightperf = $widthf * $getsizesperf[1];
$newheightperf = $newheightperf / $getsizesperf[0];
$newwidthperf = $widthf;
}
}
$thumbperf = imagecreatetruecolor($newwidthperf, $newheightperf);
imagecopyresampled($thumbperf, $imgperf, 0, 0, 0, 0, $newwidthperf, $newheightperf, $getsizesperf[0], $getsizesperf[1]);
if($newwidthperf == $widthf){
$x1 = 0;
$x2 = $widthf;
}else{
$sobraw = $newwidthperf - $widthf;
$mitadw = floor($sobraw / 2);
$x1 = $mitadw;
$x2 = $mitadw + $widthf;
}
if($newheightperf == $heightf){
$y1 = 0;
$y2 = $heightf;
}else{
$sobrah = $newheightperf - $heightf;
$mitadh = floor($sobrah / 2);
$y1 = $mitadh;
$y2 = $mitadh + $heighf;
}
$fpperf = imagecreatetruecolor ($widthf, $heightf);
imagecopy($fpperf, $thumbperf, 0, 0, $x1, $y1, $widthf, $heightf);
imagejpeg($fpperf,"",100);
imagedestroy($fpperf);
}
Ya me contaras!