简体   繁体   English

如何仅使用PHP压缩尚未压缩的图像?

[英]How do I compress only images that haven't been compressed already, using just PHP?

I'm currently trying to speed up the websites we develop. 我目前正在尝试加快我们开发的网站的速度。 The part I'm working on now is to optimise the images so that they are as small (filesize, not dimensions) as possible without losing quality. 我现在正在研究的部分是优化图像,以使其尽可能小(文件大小,而不是尺寸)而不会降低质量。

Our customers can upload their own images to the website through our custom CMS, but images aren't being compressed or optimised at all. 我们的客户可以通过我们的自定义CMS将自己的图像上传到网站,但是图像并没有被压缩或优化。 My superior explained this is because the customers can upload their own images, and these images could be optimised beforehand through Photoshop or tools like it. 我的上司解释说,这是因为客户可以上传自己的图像,并且可以通过Photoshop或类似的工具预先优化这些图像。 If you optimise already optimised images, the quality would get worse. 如果优化已经优化的图像,质量会变差。 ...right? ...对?

We're trying to find a solution that won't require us to install a module or anything. 我们正在尝试寻找一种不需要我们安装模块或任何东西的解决方案。 We already use imagejpeg() , imagepng() and imagegif() , but we don't use the $quality parameter because of reasons previously explained. 我们已经使用了imagejpeg()imagepng()imagegif() ,但是由于先前说明的原因,我们没有使用$quality参数。 I've seen some snippets, but they all use imagejpg() and the like. 我已经看到了一些片段,但是它们都使用imagejpg()之类的东西。

That being said, is there a sure-fire way of optimising images without the risk of optimising previously optimised images? 话虽这么说,是否有确保图像优化的可靠方法,而又没有优化先前优化图像的风险? Or would it be no problem at all to use imagejpeg() , imagepng() and imagegif() , even if it would mean optimising already optimised images? 还是使用imagejpeg()imagepng()imagegif()根本没有问题,即使这意味着优化已经优化的图像也是如此吗?

Thank you! 谢谢!

"If you optimise already optimised images, the quality would get worse. " No if you use a method without loose. “如果优化已经优化的图像,质量会变差。”如果您使用的方法不松懈,那就不会。

I don't know for method directly in php but if you are on linux server you can use jpegtran or jpegoptim ( with --strip-all) for jpeg and OptiPNG or PNGOUT for png. 我不知道直接在php中使用方法,但是如果您在linux服务器上,则可以对jpeg使用jpegtran或jpegoptim(带有--strip-all),对png使用OptiPNG或PNGOUT。

Going from your title, I am going to assume compression 从你的头衔开始,我要假设压缩

So, lets say a normal jpg of 800x600 is uploaded by your customers. 因此,可以说您的客户上传了800x600的常规jpg。 The customers jpg is 272kb because it has full details and everything. 客户jpg为272kb,因为它具有完整的详细信息和所有内容。

You need to set tresholds for filesizes at dimensions what is acceptable. 您需要以可接受的尺寸设置文件大小的阈值。

Like: 喜欢:

if $dimensions->equals(800,600) and file_type($image) =='jpg' and file_size($image) > 68kb 
then schedule_for_compression($image)

and that way you set up parameters for what is acceptable as an upper limit of file size. 这样,您就可以设置可接受的文件大小上限参数。 If the dimensions match, and the filesize is bigger, then its not optimised. 如果尺寸匹配,并且文件大小较大,则未进行优化。

But without knowing more details what exactly is understood about optimising, this is the only thing I can think of. 但是,在不了解更多细节的情况下,对优化的理解是什么,这是我唯一能想到的。

If you are using a low number of images to compress, you might find an external service, such as: https://tinypng.com/developers might be of assistance. 如果要压缩的图像数量较少,则可能会找到外部服务,例如: https : //tinypng.com/developers可能会有所帮助。

I've used their on-line tools for reducing filesize on both JPG and PNG file manually but they do appaear to offer a free API service for the first 500 images per month. 我使用了他们的在线工具来手动减少JPG和PNG文件的文件大小,但是他们确实为每月的前500张图像提供免费的API服务。

Apologies if this would be better as a comment than an answer, I'm fairly new to stackoverflow and haven't got enough points yet, but felt this may be a handy alternative solution. 抱歉,如果将其作为注释比回答更好,我对stackoverflow还是陌生的,还没有足够的观点,但是觉得这可能是一个方便的替代解决方案。

Saving a JPEG with the same or higher quality setting will not result in a noticeable loss in quality. 以相同或更高的质量设置保存JPEG不会导致质量明显下降。 Just re-save with your desired quality setting. 只需重新保存您想要的质量设置即可。 If the file ends up larger, just discard it and keep the original. 如果文件较大,则将其丢弃并保留原始文件。 Remove metadata using jpegran or jpegoptim before you optimize so it doesn't affect the file size when you compare to the original. 在进行优化之前,请使用jpegran或jpegoptim删除元数据,以便在与原始文件进行比较时不会影响文件大小。

PNG and GIF wont't lose any quality unless you reduce the number of colors. 除非您减少颜色数量,否则PNG和GIF不会丢失任何质量。 Just use one of the optimizers Gyncoca mentioned. 只需使用Gyncoca提到的优化程序之一即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM