如果要生成缩略图,则必须首先使用调整图像大小
imagecopyresampled();。您必须调整图像的大小,以使图像较小侧的尺寸等于拇指的相应侧。
例如,如果源图像为1280x800px,拇指为200x150px,则必须将图像调整为240x150px,然后将其裁剪为200x150px。这样一来,图像的长宽比就不会改变。
这是创建缩略图的一般公式:
$image = imagecreatefromjpeg($_GET['src']);$filename = 'images/cropped_whatever.jpg';$thumb_width = 200;$thumb_height = 150;$width = imagesx($image);$height = imagesy($image);$original_aspect = $width / $height;$thumb_aspect = $thumb_width / $thumb_height;if ( $original_aspect >= $thumb_aspect ){ // If image is wider than thumbnail (in aspect ratio sense) $new_height = $thumb_height; $new_width = $width / ($height / $thumb_height);}else{ // If the thumbnail is wider than the image $new_width = $thumb_width; $new_height = $height / ($width / $thumb_width);}$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );// Resize and cropimagecopyresampled($thumb, $image, 0 - ($new_width - $thumb_width) / 2, // Center the image horizontally 0 - ($new_height - $thumb_height) / 2, // Center the image vertically 0, 0, $new_width, $new_height, $width, $height);imagejpeg($thumb, $filename, 80);还没有测试过,但是 应该 可以。
编辑
现在经过测试并可以工作。



