使用 php 创建 jpg 拇指
我在从上传的图像创建缩略图时遇到问题,我的问题是
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次创建缩略图时,图像的 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
您使用了错误的图像高度变量。
应该是:
这应该可以提高图像质量,但是您应该使用 imagecopyresampled 来调整大小,并在保存到磁盘时使用 imagejpeg() 函数时使用质量参数。
You're using the wrong variable for the image height.
Should be:
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.
如果您使用缩略图器,您就不用担心。
质量非常好。您也可以将拐角弄圆。
You would not worry if you would use the Thumbnailer.
The quality is superb. You can also round the corners.