3 回答

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果要生成縮略圖,則必須首先使用調(diào)整圖像大小imagecopyresampled();。您必須調(diào)整圖像的大小,以使圖像較小側(cè)的尺寸等于拇指的相應(yīng)側(cè)。
例如,如果源圖像為1280x800px,拇指為200x150px,則必須將圖像的尺寸調(diào)整為240x150px,然后將其裁剪為200x150px。這樣一來(lái),圖像的長(zhǎng)寬比就不會(huì)改變。
這是創(chuàng)建縮略圖的一般公式:
$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 crop
imagecopyresampled($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);
還沒(méi)有測(cè)試過(guò),但是應(yīng)該可以。
編輯
現(xiàn)在經(jīng)過(guò)測(cè)試并可以工作。
- 3 回答
- 0 關(guān)注
- 411 瀏覽
添加回答
舉報(bào)