裁剪缩略图图像失败

时间:2019-06-29 07:31:50

标签: php image crop

我的图片如下:

image1

我正在尝试从中创建缩略图,该缩略图应显示如下。如您所见,第二张图像要小得多,但不仅是调整了大小,还放大了图像的主要部分以强调框架。

image2

我尝试了在此处其他问题中找到的许多图像调整大小/裁剪解决方案,但它们似乎仅调整图像的大小,而实际上并未裁剪出我需要的部分。

我在使用imagefromjpeg()等时尝试了很多功能,但到目前为止都失败了。

我尝试过的例子:

$target_width  = 340;
$target_height = 270;
$new_img = @imagecreatetruecolor($target_width, $target_height);

$width_ratio  = $target_width  / $img_width;
$height_ratio = $target_height / $img_height;
if($width_ratio > $height_ratio) {
  $resized_width  = $target_width;
  $resized_height = $img_height * $width_ratio;
} else {
  $resized_height = $target_height;
  $resized_width  = $img_width * $height_ratio;
}

$resized_width  = round($resized_width);
$resized_height = round($resized_height);


$offset_width  = round(($target_width  - $resized_width) / 2);
$offset_height = round(($target_height - $resized_height) / 2);

$imgCopyRes = @imagecopyresampled($new_img, $src_img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $img_width, $img_height);

0 个答案:

没有答案
相关问题