哪个是在不损失质量的情况下减小图像大小的最佳 PHP 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11418594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is the best PHP method to reduce the image size without losing quality
提问by user1431849
I am trying to develop an image based web site. I am really confused about the image type and compression techniques. Please advice me the best way to compress image sizes.
我正在尝试开发一个基于图像的网站。我真的对图像类型和压缩技术感到困惑。请建议我压缩图像大小的最佳方法。
采纳答案by Tudor Constantin
I'd go for jpeg. Read this postregarding image size reduction and after deciding on the technique, use ImageMagick
我会去jpeg。阅读这篇关于缩小图像尺寸的文章,并在决定技术后,使用ImageMagick
Hope this helps
希望这可以帮助
回答by Vengat Owen
If you are looking to reduce the size using coding itself, you can follow this code in php.
如果您希望使用编码本身来减小大小,您可以在 php.ini 中遵循此代码。
<?php
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
$source_img = 'source.jpg';
$destination_img = 'destination .jpg';
$d = compress($source_img, $destination_img, 90);
?>
$d = compress($source_img, $destination_img, 90);
This is just a php function that passes the source image ( i.e., $source_img), destination image ( $destination_img) and quality for the image that will take to compress ( i.e., 90 ).
这只是一个 php 函数,它传递源图像(即$source_img)、目标图像($destination_img)和要压缩的图像的质量(即 90)。
$info = getimagesize($source);
The getimagesize()function is used to find the size of any given image file and return the dimensions along with the file type.
该getimagesize()函数用于查找任何给定图像文件的大小,并返回尺寸和文件类型。
回答by maxhud
You can resize and then use imagejpeg()
您可以调整大小,然后使用 imagejpeg()
Don't pass 100 as the quality for imagejpeg() - anything over 90 is generally overkill and just gets you a bigger JPEG. For a thumbnail, try 75 and work downwards until the quality/size tradeoff is acceptable.
不要通过 100 作为 imagejpeg() 的质量 - 任何超过 90 的东西通常都是过大的,只会让您获得更大的 JPEG。对于缩略图,尝试 75 并向下工作,直到质量/大小权衡可以接受为止。
imagejpeg($tn, $save, 75);
回答by whizzzkid
well I think I have something interesting for you... https://github.com/whizzzkid/phpimageresize. I wrote it for the exact same purpose. Highly customizable, and does it in a great way.
好吧,我想我有一些有趣的东西给你... https://github.com/whizzzkid/phpimageresize。我写它是为了完全相同的目的。高度可定制,并且做得很好。

