如何避免优化已经使用PHP优化的图像? [英] How to avoid Optimizing images that are already optimized with PHP?

查看:125
本文介绍了如何避免优化已经使用PHP优化的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个PHP应用程序,它从命令行运行以优化Images的文件夹。

I am currently working on a PHP application which is ran from the command line to optimize a folder of Images.

PHP应用程序更像是其他的包装器Image Optimizer和它只是迭代目录并抓取所有图像,然后通过适当的程序运行Image以获得最佳结果。

The PHP application is more of a wrapper for other Image Optimizer's and it simply iterates the directory and grabs all the images, it then runs the Image through the appropriate program to get the best result.

以下是我的程序将使用以及每个将用于...

Below are the Programs that I will be using and what each will be used for...

imagemagick 确定文件类型并转换非动画gif的到png

gifsicle 优化动画GIF 图片

jpegtran 优化 jpg 图片

pngcrush 优化 png 图片

pngquant png 图片优化为png8格式

pngout png 图片优化为png8格式

imagemagick to determine file type and convert non-animated gif's to png
gifsicle to optimize Animated Gif images
jpegtran to optimize jpg images
pngcrush to optimize png images
pngquant to optimize png images to png8 format
pngout to optimize png images to png8 format

我的问题:1-10 IM年龄,一切都运行平稳,相当快,但是,一旦我在一个10个或更多图像的较大文件夹上运行,它变得非常慢。我并没有真正看到一个很好的解决方案,但有一点可以帮助避免重新处理已经优化的图像。因此,如果我有一个包含100个图像的文件夹并优化该文件夹然后添加5个新图像,请重新运行优化程序。然后它必须优化105个图像,我的目标是让它只优化5个较新的图像,因为之前的100个图像已经被优化。仅当将新图像添加到图像文件夹时,这将极大地提高性能。

My problem: With 1-10 images, everything runs smooth and fairly fast however, once I run on a larger folder with 10 or more images, it becomes really slow. I do not really see a good solution around this but one thing that would help is to avoid re-processing images that have already been Optimized. So if I have a folder with 100 images and I optimize that folder and then add 5 new images, re-run the optimizer. It then has to optimize 105 images, my goal is to have it only optimize the 5 newer images since the previous 100 would have already been optimized. This alone would greatly improve performance when new images are added to the image folder.

我意识到简单的解决方案是简单地将图像复制或移动到新文件夹之后处理它们,我的问题在于这个简单的解决方案是这些图像用于网络和网站,因此图像通常硬链接到网站源代码,并且改变图像的路径会使其复杂化并且有时可能会破坏它。

I realize the simple solution would be to simply copy or move the images to a new folder after processing them, my problem with that simple solution is that these images are used for the web and websites, so the images are generally hard-linked into a websites source code and changing the path to the images would complicate that and possibly break it sometimes.

我的一些想法是:将某种文本文件数据库写入图像文件夹,列出所有已经处理过的图像,所以当应用程序是运行,它只会在不在该文件中的图像上运行。另一个想法是将文件名标记为在名称中显示某种标识以表明它已经过优化,第三个想法是在优化后将每个优化文件移动到最终目标文件夹。想法2和3虽然不好,但因为它们会破坏网站源代码中的所有图像路径链接。

Some ideas I have had are: Write some kind of text file database to the image folders that will list all the images that have already been processed, so when the application is ran, it will only run on images that are not in that file already. Another idea was to cheange the file name to have some kind of identification in the name to show it has been optimized, a third idea is to move each optimized file to a final destination folder once it is optimized. Idea 2 and 3 are not good though because they will break all image path links in the websites source code.

所以,如果你能想到一个体面/好的解决方案,请这个问题请分享?

So please if you can think of a decent/good solution to this problem, please share?

推荐答案

元数据

你可以把优化后每个图像的元信息中的标志。首先检查该标志,只有在不存在时才进行。您可以使用 exif_read_data() 读取数据。写它可能 像这样

Meta data
You could put a flag in the meta info of each image after it is optimized. First check for that flag and only proceed if it's not there. You can use exif_read_data() to read the data. Writing it maybe like this.

上述内容适用于JPG。用于PNG的Metdata也可以看看 这个问题, 这一个

The above is for JPGs. Metdata for PNGs is also possible take a look at this question, and this one.

我不确定GIF,但你绝对可以 将它们转换为PNG ,然后添加元数据...虽然我很确定他们有自己的元信息,因为 元数据提取工具 允许使用GIF。

I'm not sure about GIFs, but you could definitely convert them to PNGs and then add metadata... although I'm pretty sure they have their own meta info, since meta data extraction tools allow GIFs.

数据库支持

另一种解决方案是将有关图像的信息存储在MySQL数据库中。这样,在调整优化时,您可以跟踪何时以及在哪个图像上尝试优化。您可以根据您选择的任何参数选择要优化的图像。您可以为此构建一个管理面板。这种方法可以轻松进行实验。

Database Support
Another solution would be to store information about the images in a MySQL database. This way, as you tweak your optimizations you could keep track of when and which optimization was tried on which image. You could pick which images to optimize according to any parameters of your choosing. You could build an admin panel for this. This method would allow easy experimentation.

你也可以结合上述两种方法。

You could also combine the above two methods.

最大值文件大小

由于这是为了节省空间,因此您可以让程序仅处理大于特定文件大小的图像。理想情况下,在运行一次压缩器后,所有图像都将低于此文件大小,之后只会触摸新添加的太大的图像。我不知道这在实现方面有多实用,因为它需要压缩器获得低于某些任意文件大小的任何图像。您可以根据图像大小确定最大文件大小.....

Maximum File Size
Since this is for saving space, you could have the program only work on images that are larger than a certain file size. Ideally, after running the compressor once, all the images would be below this file size, and after that only newly added images that are too big would be touched. I don't know how practical this is in terms of implementation, since it would require that the compressor gets any image below some arbitrary files size. You could make the maximum file size dependent on image size.....

这篇关于如何避免优化已经使用PHP优化的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆