我尝试使用gd库创建缩略图,但我的缩略图总是失真。我想要的是从中心开始切割图像,尊重新的分辨率并丢失旧比例。
我尝试过的事情:
$im = ImageCreateFromJPEG($target_file);
$n_width = 500; // Fix the width of the thumb nail images
$n_height = 500; // Fix the height of the thumb nail imaage
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
$newimage = imagecreatetruecolor($n_width, $n_height);
imagecopyresampled($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
$thumb_target = $target_dir . $filename_without_ext . '-thumb.' . $params['file_ext'];
ImageJpeg($newimage, $thumb_target);
chmod("$thumb_target", 0777);
尝试将imagecreatetruecolor
更改为imagecrop
,但仍然没有采取我想要的行为。
如果我不够清楚,请告诉我。
答案 0 :(得分:2)
使用ImageManipulator库解决了它。在this answer中找到了解决方案。
我的最终代码:
$im = new ImageManipulator($target_file);
$centreX = round($im->getWidth() / 2);
$centreY = round($im->getHeight() / 2);
$x1 = $centreX - 500;
$y1 = $centreY - 500;
$x2 = $centreX + 500;
$y2 = $centreY + 500;
$im->crop($x1, $y1, $x2, $y2);
$im->save($target_dir . $filename_without_ext . '-thumb.' . $params['file_ext']);
答案 1 :(得分:1)
好吧,正如你所说,你想裁剪这张照片。
有一个例子,“如何”右here
答案 2 :(得分:0)
你的新尺寸产生正方形;这将始终扭曲缩略图,除非原始图像(非常接近)方形。如果您从旧计算新维度(找到较大的维度,除以500像素,将较小的乘以相同的比率),您将获得具有相同比例的非方形缩略图,因此它看起来不会失真。
如果你需要拇指是方形的,你可以将较小的尺寸转换为500像素,按照我上面的建议创建图像,然后裁剪到500x500。