2009-10-07 13 views
7

Voy a crear imágenes en miniatura de altura fija y el ancho de mi script PHP usando la siguiente funcióntamaño de imagen tema en PHP - GD crea imágenes redimensionadas feas

/*creates thumbnail of required dimensions*/ 
function createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false) 
{ 
    /* 
    * $sourcefilepath = absolute source file path of jpeg 
    * $destdir = absolute path of destination directory of thumbnail ending with "/" 
    */ 
    $thumbWidth = $reqwidth; /*pixels*/ 
    $filename = split("[/\\]",$sourcefilepath); 
    $filename = $filename[count($filename)-1]; 
    $thumbnail_path = $destdir.$filename; 
    $image_file = $sourcefilepath; 

    $img = imagecreatefromjpeg($image_file); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    if($aspectratio==true) 
    { 
     $new_height = floor($height * ($thumbWidth/$width)); 
    } 
    else 
    { 
     $new_height = $reqheight; 
    } 

    // create a new temporary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 

    $returnvalue = imagejpeg($tmp_img,$thumbnail_path); 
    imagedestroy($img); 
    return $returnvalue; 
} 

y llamar a esta función con los parámetros siguientes

createThumbnailofSize($sourcefilepath,$destdir,48,48,false); 

pero el problema es que la imagen resultante es de muy mala calidad, cuando realizo la misma operación con Adobe Photo Shop, realiza una buena conversión ... ¿por qué es así? No puedo encontrar ningún parámetro de calidad, a través del cual cambio la calidad de la imagen de salida.

Respuesta

25
+2

imagecopyresampled() utiliza el algoritmo de cambio de tamaño bicúbico – Jacco

+0

Muchas gracias, déjame probar esta función –

+2

Otro punto importante: si algunas miniaturas son totalmente negras. El problema puede provenir de algunas cámaras y teléfonos celulares que agregan algunos caracteres adicionales. Para evitar este problema, agregue esto: 'ini_set (" gd.jpeg_ignore_warning ", 1);'. Entonces solo recibirá esta advertencia 'Datos JPEG corruptos: 2 bytes extraños antes del marcador 0xd9' pero la miniatura se generará correctamente. – Toto

1

si se trata de la calidad de imagen que está después que necesita para dar el parámetro de calidad cuando se guarda la imagen utilizando imagejpeg ($ tmp_img, $ thumbnail_path, 100) // valor por defecto es 75

/*creates thumbnail of required dimensions*/ 
function 
createThumbnailofSize($sourcefilepath,$destdir,$reqwidth,$reqheight,$aspectratio=false) 
{ 
    /* 
    * $sourcefilepath = absolute source file path of jpeg 
    * $destdir = absolute path of destination directory of thumbnail ending with "/" 
    */ 
    $thumbWidth = $reqwidth; /*pixels*/ 
    $filename = split("[/\\]",$sourcefilepath); 
    $filename = $filename[count($filename)-1]; 
    $thumbnail_path = $destdir.$filename; 
    $image_file = $sourcefilepath; 

$img = imagecreatefromjpeg($image_file); 
$width = imagesx($img); 
$height = imagesy($img); 

// calculate thumbnail size 
$new_width = $thumbWidth; 
if($aspectratio==true) 
{ 
    $new_height = floor($height * ($thumbWidth/$width)); 
} 
else 
{ 
    $new_height = $reqheight; 
} 

// create a new temporary image 
$tmp_img = imagecreatetruecolor($new_width, $new_height); 

// copy and resize old image into new image 
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// save thumbnail into a file 

$returnvalue = imagejpeg($tmp_img,$thumbnail_path,100); 
imagedestroy($img); 
return $returnvalue; 

}

+0

El solo hecho de aumentar el parámetro Calidad no ayuda. remuestreo mejora la calidad – rjha94

1

También podría considerar el uso de ImageMagick (http://us3.php.net/manual/en/book.imagick.php) en lugar de Gd. Tuve el mismo problema hace solo un par de días con Java. Ir por ImageMagick en lugar de imágenes avanzadas de Java resultó en una gran diferencia de calidad .

0

Es posible que también desee echar un vistazo a Image_Transform PEAR package. Cuida muchos de los detalles de bajo nivel para usted y hace que la creación y maniuplicación de imágenes sean indoloras. También le permite usar bibliotecas GD o ImageMagick. Lo he usado con gran éxito en varios proyectos.

0

intentado con el php.Thumbnailer?

$thumb=new Thumbnailer("photo.jpg"); 
$thumb->thumbSquare(48)->save("thumb.jpg"); 

La foto de resultados será 48x48px. Fácil, ¿verdad? :)

+0

Piensa que el enlace está roto –

Cuestiones relacionadas