使用 php 创建 jpg 拇指

发布于 2024-08-28 08:36:51 字数 1372 浏览 3 评论 0原文

我在从上传的图像创建缩略图时遇到问题,我的问题是

(i) 质量 (ii) 裁剪

http://welovethedesign.com.cluster.cwcs .co.uk/phpimages/large.jpg http://welovethedesign.com.cluster.cwcs.co.uk/ phpimages/thumb.jpg

如果您看到质量非常差,并且裁剪是从顶部拍摄的,并且不是原始图像的调整大小,尽管尺寸意味着它是按比例的。

原件宽 1600px,高 1100px

任何帮助将不胜感激。

 $thumb =
 $targetPath."Thumbs/".$fileName;

    $imgsize =
 getimagesize($targetFile);  $image =
 imagecreatefromjpeg($targetFile);
  $width = 200; //New width of image   
 $height = 138; //This maintains
 proportions

  $src_w = $imgsize[0]; $src_h =
 $imgsize[1];



 $thumbWidth = 200;  $thumbHeight =
 138; // Intended dimension of thumb

 // Beyond this point is simply code.

 $sourceImage =
 imagecreatefromjpeg($targetFile);
 $sourceWidth = imagesx($sourceImage);
 $sourceHeight = imagesy($sourceImage);

 $targetImage =
 imagecreate($thumbWidth,$thumbHeight);
 imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));

 //imagejpeg($targetImage,
 "$thumbPath/$thumbName");
 imagejpeg($targetImage, $thumb);

 chmod($thumb, 0755);

I am having problems creating a thumbnail from an uploaded image, my problem is

(i) the quality
(ii) the crop

http://welovethedesign.com.cluster.cwcs.co.uk/phpimages/large.jpg
http://welovethedesign.com.cluster.cwcs.co.uk/phpimages/thumb.jpg

If you look the quality is very poor and the crop is taken from the top and is not a resize of the original image although the dimesions mean it is in proportion.

The original is 1600px wide by 1100px high.

Any help would be appreciated.

 $thumb =
 $targetPath."Thumbs/".$fileName;

    $imgsize =
 getimagesize($targetFile);  $image =
 imagecreatefromjpeg($targetFile);
  $width = 200; //New width of image   
 $height = 138; //This maintains
 proportions

  $src_w = $imgsize[0]; $src_h =
 $imgsize[1];



 $thumbWidth = 200;  $thumbHeight =
 138; // Intended dimension of thumb

 // Beyond this point is simply code.

 $sourceImage =
 imagecreatefromjpeg($targetFile);
 $sourceWidth = imagesx($sourceImage);
 $sourceHeight = imagesy($sourceImage);

 $targetImage =
 imagecreate($thumbWidth,$thumbHeight);
 imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));

 //imagejpeg($targetImage,
 "$thumbPath/$thumbName");
 imagejpeg($targetImage, $thumb);

 chmod($thumb, 0755);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-09-04 08:36:52

每次创建缩略图时,图像的 DPI 都必须变低,因此不可能具有相同的质量,但是您可以检查 imagecreatetruecolor (https://www.php.net/manual/en/function.imagecreatetruecolor.php )进行改进

Every time u create a thumbnail the DPI of the image has to go low and thus it is not possible to have the same quality, however u can check imagecreatetruecolor (https://www.php.net/manual/en/function.imagecreatetruecolor.php ) for improvement

幸福不弃 2024-09-04 08:36:52

您使用了错误的图像高度变量。

imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));

应该是:

imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));

这应该可以提高图像质量,但是您应该使用 imagecopyresampled 来调整大小,并在保存到磁盘时使用 imagejpeg() 函数时使用质量参数。

You're using the wrong variable for the image height.

imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));

Should be:

imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));

This should improve image quality but you should use imagecopyresampled for resizing and use the quality parameter when using the imagejpeg() function when saving to disk.

把梦留给海 2024-09-04 08:36:52

如果您使用缩略图器,您就不用担心。

$th=new Thumbnailer("your-photo.jpg");
$th->thumbSymmetricWidth(200)->save("your-thumb.jpg");

质量非常好。您也可以将拐角弄圆。

You would not worry if you would use the Thumbnailer.

$th=new Thumbnailer("your-photo.jpg");
$th->thumbSymmetricWidth(200)->save("your-thumb.jpg");

The quality is superb. You can also round the corners.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文