If you want to create a thumbnail, apply a filter to an image or transform it in any other way, you will have to employ an image processing library in your PHP application. It means that you will probably choose GD or ImageMagick. But which one supports a wider range of image formats? Maybe one of them is slower than the other? What other criteria should be taken under consideration when choosing the right library? Read the article to find out!
如果要创建缩略图,对图像应用过滤器或以任何其他方式对其进行转换,则必须在PHP应用程序中使用图像处理库。 这意味着您可能会选择GD或ImageMagick 。 但是,哪一种支持更广泛的图像格式呢? 也许其中一个比另一个慢? 选择正确的库时应考虑哪些其他标准? 阅读文章以了解答案!
Both GD and ImageMagick are available in PHP on the condition that they were installed and configured along with PHP itself. The GD library is included by default since PHP 4.3 so you will probably be able to use it in your project in most server environments. On the other hand, ImageMagick may not always be available and some of the hosting companies don't include it in their offer.
GD和ImageMagick都可以在PHP中使用,但前提是它们必须与PHP一起安装和配置。 自PHP 4.3起,默认情况下已包含GD库,因此您可能可以在大多数服务器环境中的项目中使用它。 另一方面,ImageMagick可能并不总是可用,并且某些托管公司未将其包括在报价中。
You can run a few lines of code to check the availability of both libraries. The ImageMagick queryFormats() and GD gd_info() functions also list the image formats supported by each of the libraries:
您可以运行几行代码来检查两个库的可用性。 ImageMagick queryFormats()和GD gd_info()函数还列出了每个库支持的图像格式:
if(extension_loaded('gd')) { print_r(gd_info()); } else { echo 'GD is not available.'; } if(extension_loaded('imagick')) { $imagick = new Imagick(); print_r($imagick->queryFormats()); } else { echo 'ImageMagick is not available.'; }The list of supported image formats that will be printed out after executing the code is the first sign that the ImageMagick library offers much more functionality than the other one. GD only supports JPG, PNG, GIF, WBMP, WebP, XBM and XPM files, which is not much comparing to over a hundred file types handled by the ImageMagick library.
执行代码后将打印出的受支持图像格式列表是ImageMagick库提供比其他库更多功能的第一个迹象。 GD仅支持JPG,PNG,GIF,WBMP,WebP,XBM和XPM文件,与ImageMagick库处理的一百多种文件类型相比,这并不多。
You may think that you will probably never use all of these uncommon filetypes supported by ImageMagick but this may not be true. In one of my projects I had to switch from GD to ImageMagick just because the first one doesn't support TIFF files.
您可能认为您可能永远不会使用ImageMagick支持的所有这些不常见的文件类型,但这可能并非如此。 在我的一个项目中,由于第一个不支持TIFF文件,我不得不从GD切换到ImageMagick。
Both GD and ImageMagick offer some basic functionality such as: – resizing and cropping images, – creating images that are composed of custom shapes, text and other image files, – applying image filters (changing the brightness, contrast, colorizing etc.).
GD和ImageMagick均提供一些基本功能,例如:–调整图像大小和裁剪图像; –创建由自定义形状,文本和其他图像文件组成的图像; –应用图像滤镜(更改亮度,对比度,着色等)。
If you want to process images in a more advanced way, check all the features of the ImageMagick library. As shown at the ImageMagic example pages – the first one and the second – you can transform the image, decorate it or distort it in countless ways.
如果要以更高级的方式处理图像,请检查ImageMagick库的所有功能。 如ImageMagic示例页面( 第一个和第二个)上所示 ,您可以无数种方式变换,修饰或扭曲图像。
The PHP ImageMagick class itself offers 331 methods which is quite an impressive number (no, I didn't count them manually, I used the ReflectionClass ;)). On one hand it shows great reach of the ImageMagick library, while on the other it makes it difficult to find and implement the proper method for a specific use case.
PHP ImageMagick类本身提供了331个方法 ,这是一个令人印象深刻的数字(不,我没有手动计算它们,我使用了ReflectionClass ;))。 一方面,它显示了ImageMagick库的强大功能,而另一方面,它使得很难为特定用例找到并实现正确的方法。
To tell the truth, if you just want to create a set of thumbnails or apply a simple transformation to an image, you shouldn't care about comparing the performance of each of the image processing libraries.
实话说,如果您只想创建一组缩略图或对图像进行简单的转换,则不必关心比较每个图像处理库的性能。
In a series of tests that I ran on a typical server configuration, creating a thumbnail from a digital camera 3MB JPG image took around 0,6s using ImageMagick and aroung 0,5s using GD. So the whole process doesn't take much time no matter which library is used. And after browsing the web and looking for speed tests of both libraries you will quickly notice that none of them stands out in terms of performance. Sometimes the GD library is the one that works faster, sometimes it is ImageMagick – it simply depends on the use case. Don't take this criterion as a crucial one when deciding whether to use GD or ImageMagick.
在一系列的测试,我跑了一个典型的服务器配置,从数码相机创建缩略图3MB JPG图片前后花了使用ImageMagick 0,6s和使用GD aroung 0,5s。 因此,无论使用哪个库,整个过程都不会花费很多时间。 在浏览Web并查找两个库的速度测试后,您会很快注意到,它们在性能方面均无与伦比。 有时GD库是运行速度更快的库,有时是ImageMagick –它仅取决于用例。 在决定使用GD还是ImageMagick时,不要将此标准作为关键标准。
If you compare the code responsible for the same image transformation written using the GD and the ImageMagick library, you will quickly notice that there are several differences. The GD library is available through a set of functions like getimagesize() or imagecreatetruecolor() so the whole image processing script needs to be written in a procedural style. Let's see an example of creating a JPG image thumbnail:
如果将负责使用GD和ImageMagick库编写的同一图像转换的代码进行比较,您将很快注意到存在一些差异。 GD库可通过诸如getimagesize()或imagecreatetruecolor()类的一组函数使用,因此整个图像处理脚本需要以过程样式编写。 让我们来看一个创建JPG图像缩略图的示例:
$src_img = imagecreatefromjpeg('source.jpg'); if(!$src_img) { die('Error when reading the source image.'); } $thumbnail = imagecreatetruecolor(800, 800); if(!$thumbnail) { die('Error when creating the destination image.'); } $result = imagecopyresampled($thumbnail, $src_img, 0, 0, 0, 0, 800, 800, 1600, 1600); if(!$result) { die('Error when generating the thumbnail.'); } $result = imagejpeg($thumbnail, 'destination.jpg'); if(!$result) { die('Error when saving the thumbnail.'); } $result = imagedestroy($thumbnail); if(!$result) { die('Error when destroying the image.'); }As the exceptions aren't thrown in case of an error, all the error handling has to be implemented by checking the result of each GD function. You also have to deal with monstrous functions that have ten arguments, like imagecopyresampled() or imagecopyresized(). I'm convinced that such a number of arguments is not an example of a good coding practice.
由于不会在发生错误的情况下引发异常,因此必须通过检查每个GD函数的结果来实现所有错误处理。 您还必须处理具有十个参数的可怕函数,例如imagecopyresampled()或imagecopyresized() 。 我坚信如此众多的论点并不是良好编码实践的一个例子。
Another thing that may not be very convenient is the fact that the functions responsible for reading and saving an image are different depending on the image type. So if you want your thumbnail generator script to handle different file types, you need to add a code like:
可能不太方便的另一件事是,负责读取和保存图像的功能因图像类型而异。 因此,如果您希望缩略图生成器脚本处理不同的文件类型,则需要添加如下代码:
switch($image_type) { case 'gif' : $src_img = imagecreatefromgif($path); break; case 'png' : $src_img = imagecreatefrompng($path); break; case 'jpg' : case 'jpeg' : $src_img = imagecreatefromjpeg($path); break; default: return false; break; } //continue with creating the thumbnailThen, you will have to execute different functions depending on the image type to save the target image in the proper format. As you can see, the GD code gets complicated fast.
然后,您将不得不根据图像类型执行不同的功能,以将目标图像保存为正确的格式。 如您所见,GD代码变得非常复杂。
Just look at the ImageMagick code responsible for the same operation and you will notice the difference:
只需查看负责相同操作的ImageMagick代码,您就会注意到其中的区别:
try { $imagick = new Imagick(); $imagick->readImage('source.jpg'); $imagick->thumbnailImage(800, 800); $imagick->writeImage('destination.jpg'); } catch(Exception $e) { die('Error when creating a thumbnail: ' . $e->getMessage()); }The ImageMagick library is accessible through the Imagick class. Thus, we can benefit from all the object-oriented programming paradigm advantages. The simplest example is the way of handling the errors. When using the ImageMagick library you can just wrap all the code in a try-catch block and your app can be executed safely.
可通过Imagick类访问ImageMagick库。 因此,我们可以从所有面向对象的编程范例优势中受益。 最简单的示例是处理错误的方法。 使用ImageMagick库时,您只需将所有代码包装在try-catch块中,即可安全地执行您的应用程序。
As you can see above, the ImageMagick script responsible for creating a thumbnail doesn't contain any code related to the type of the source image. The same code may be used to process JPG images as well as PNG or TIF files. And if you need to convert the source image to another type, just add one line of code before executing the writeImage() method:
如上所示,负责创建缩略图的ImageMagick脚本不包含与源图像类型有关的任何代码。 相同的代码可用于处理JPG图像以及PNG或TIF文件。 而且,如果您需要将源图像转换为另一种类型,只需在执行writeImage()方法之前添加一行代码writeImage() :
$image->setImageFormat('PNG');Isn't it just more clear? In my opinion, processing images using the GD library functions is not as handy as with ImageMagick. Of course, there are various wrappers available for GD that make it object oriented, but at that point it starts to feel like patching a patch.
这不是更清楚吗? 我认为,使用GD库功能处理图像不如使用ImageMagick方便。 当然,GD可以使用各种包装来使其面向对象,但从那时起,它开始感觉像是在修补补丁。
As the GD library is included by default in all new PHP versions, you will probably see this library in various projects more often than ImageMagick. When I needed to include in my CakePHP project a component responsible for handling image uploads and thumbnail generation, I quickly found one that suited my needs, based on GD. You may sometimes find some well written modules that let you choose between the two image processing libraries – like the Kohana framework image library, but I'm afraid they are not so frequent.
由于GD库默认包含在所有新PHP版本中,因此与ImageMagick相比,您可能会在各种项目中更频繁地看到此库。 当我需要在CakePHP项目中包含负责处理图像上载和缩略图生成的组件时,我很快就找到了一个基于GD的适合我的需求的组件。 有时您可能会找到一些编写良好的模块,可以在这两个图像处理库之间进行选择,例如Kohana框架图像库 ,但恐怕它们并不是那么频繁。
When deciding how to handle the image processing in your application, you don't need to stick with one PHP library or another. There are other solutions worth considering:
在决定如何处理应用程序中的图像处理时,您无需坚持使用一个或另一个PHP库。 还有其他解决方案值得考虑:
1. Use an image processing script that works outside the PHP application. In one of my apps I had to create a web page allowing a visitor to transform an image online, just in the browser window. I decided to use the Caman.js JavaScript image processing library which did the job very well. The library may also be employed as a background script embedded within the node.js platform which has been steadily gaining popularity.
1.使用在PHP应用程序之外运行的图像处理脚本。 在我的一个应用程序中,我必须创建一个网页,允许访问者仅在浏览器窗口中就可以在线转换图像。 我决定使用Caman.js JavaScript图像处理库,它做得很好。 该库还可以用作嵌入在node.js平台中的后台脚本,该脚本已经稳步流行。
2. Employ a cloud-based image processing platform. A cloud based solution can do the job for you – after sending the source file you can fetch different size thumbnails or images transformed by various filters. You don't need to write too much code and you're not limited by your server capabilities. Just open Google to find some companies that offer such services.
2.使用基于云的图像处理平台。 基于云的解决方案可以为您完成任务-发送源文件后,您可以获取不同大小的缩略图或由各种过滤器转换的图像。 您不需要编写太多的代码,并且不受服务器功能的限制。 只需打开Google,即可找到一些提供此类服务的公司。
3. Check the functionalities of the components that you're already using. You may be suprised to find that you can transform your images by employing a service already connected to your application. For example, the Dropbox API offers the thumbnails method that allows you to fetch a JPG or PNG image in one of five available dimensions. Check your libraries' and APIs' documentation and maybe you will find out that they can do the things you need.
3.检查您正在使用的组件的功能。 您可能会惊讶地发现,可以通过使用已经连接到应用程序的服务来转换图像。 例如, Dropbox API提供了thumbnails方法,该方法允许您以五个可用尺寸之一获取JPG或PNG图像。 检查您的库和API的文档,也许您会发现它们可以满足您的需要。
As you can see, each of the image processing libraries has its pros and cons. The GD library is widely available so it will probably work everywhere. As it's popular, you will easily find a lot of examples and components using this library. Getting some help may also be easier as more people may be familiar with the GD library than with ImageMagick.
如您所见,每个图像处理库都有其优缺点。 GD库广泛可用,因此它可能在任何地方都可以使用。 由于它很流行,因此您可以使用该库轻松找到许多示例和组件。 获得更多帮助也可能会更容易,因为与ImageMagick相比,更多的人可能熟悉GD库。
ImageMagick supports more file types and can transform the images in a lot more ways than the GD library. It also allows you to write code of a higher clarity and quality.
ImageGDick支持更多文件类型,并且可以比GD库以更多方式转换图像。 它还允许您编写更高清晰度和质量的代码。
Finally, there are alternatives such as cloud image processing services which might eliminate the need for either of these completely. I hope this article helps you in your choice.
最后,还有其他选择,例如云图像处理服务,可能会完全消除对这两种方法的需求。 希望本文对您有所帮助。
If you have any questions or comments regarding the article, feel free to post a comment below or contact me through Google+.
如果您对本文有任何疑问或评论,请随时在下面发表评论,或通过Google+与我联系。
翻译自: https://www.sitepoint.com/imagick-vs-gd/
相关资源:jdk-8u281-windows-x64.exe