Aquí hay una función que he desarrollado para mi sitio, es posible que desee utilizar. Está basado en tu respuesta anterior.
Hace otras cosas, no solo el procesamiento de imagen - por favor eliminar todo lo que es innecesario.
<?php
$thumb_width = 500;
$thumb_height = 500;
if ($handle = opendir('to-do')) {
echo "Directory handle: $handle<br />";
echo "Files:<br /><br />";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
if (($file != ".") && ($file != "..")){
echo "$file";
$original_path = "to-do/" . $file;
$source_image = ImageCreateFromJPEG($original_path);
$thumb_width = $thumb_width;
$thumb_height = $thumb_height;
// Create the image, of the required size
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height);
if($thumbnail === false) {
//creation failed -- probably not enough memory
return null;
}
// Fill the image with a white color (this will be visible in the padding around the image,
// if the aspect ratios of the image and the thumbnail do not match)
// Replace this with any color you want, or comment it out for black.
// I used grey for testing =)
$fill = imagecolorallocate($thumbnail, 255, 255, 255);
imagefill($thumbnail, 0, 0, $fill);
// Compute resize ratio
$hratio = $thumb_height/imagesy($source_image);
$wratio = $thumb_width/imagesx($source_image);
$ratio = min($hratio, $wratio);
// If the source is smaller than the thumbnail size,
// Don't resize -- add a margin instead
// (that is, dont magnify images)
if ($ratio > 1.0)
$ratio = 1.0;
// Compute sizes
$sy = floor(imagesy($source_image) * $ratio);
$sx = floor(imagesx($source_image) * $ratio);
// Compute margins
// Using these margins centers the image in the thumbnail.
// If you always want the image to the top left, set both of these to 0
$m_y = floor(($thumb_height - $sy)/2);
$m_x = floor(($thumb_width - $sx)/2);
// Copy the image data, and resample
// If you want a fast and ugly thumbnail, replace imagecopyresampled with imagecopyresized
if (!imagecopyresampled($thumbnail, $source_image,
$m_x, $m_y, //dest x, y (margins)
0, 0, //src x, y (0,0 means top left)
$sx, $sy,//dest w, h (resample to this size (computed above)
imagesx($source_image), imagesy($source_image)) //src w, h (the full size of the original)
) {
//copy failed
imagedestroy($thumbnail);
return null;
}
/* Set the new file name */
$thumbnail_file_name = $file;
/* Apply changes on the original image and write the result on the disk */
ImageJPEG($thumbnail, $complete_path . "done/" . $thumbnail_file_name);
unset($source_image);
unset($thumbnail);
unset($original_path);
unset($targeted_image_size);
echo " done<br />";
}
}
closedir($handle);
}
?>
Así que recuerde que debe limpiarse, hay muchas secciones extraíbles. –