Graphics Library Benchmark for PHP
In the project that we are currently developing a lot of work with photographs, as a result of which the speed of the graphic library is actually the main factor in optimizing the site. It's no secret that ImageMagick has long become the standard for PHP development, although for some time a fork called GraphicsMagick has been pushing it from there. I was wondering how much they differ and how many handicaps can give the good old GD. Fortunately, just recently a graphical module for PHPixie was released that supports all three libraries, which allowed me to conduct quite interesting tests.
In the spirit of the framework itself, we will experiment on the fairy that is on the right (taken from deviantart http://j-scott-campbell.deviantart.com/) If it seems to someone that there is too much innuendo in it, then I dare to recall that for more than 20 years Lenna from the playboy has been the standard drawing for testing graphic algorithms .
Now follows a short description of the tests and the output drawings. At the end of the article there will be a benchmark of the speed of one table.
So first, try to reduce it to 100 pixels wide.
$pixie->image->read($dir.'fairy.jpg') ->resize(200) ->save($dir.'resize.jpg');
The difference is not very special at first glance, but for example, her eye on the GD version does not have that pronounced green color like that of Imagick and Gmagick.
Create a square avatar
To make an autark square, you must first scale it on the smaller side and then crop it on the larger one. On PHPixie, this is pretty simple:
$pixie->image->read($dir.'fairy.jpg') ->resize(200, 200, false) ->crop(200, 200) ->save($dir.'crop.jpg');
It is obvious that they will look identically previous.
Writing text and creating a blank image
Now let's try to create a white picture and write the text in purple using the TTF font.
$pixie->image->create(300, 70, 0xffffff, 1) ->text("Hello World", 50, $dir.'/Sofia-Regular.ttf', 10, 50, 0x5B43AA) ->save($dir.'text.jpg');
Here the results are a little more dramatic. For some reason, GD made the text color darker and antialiasing worked much worse, as a result of which the edges of the text are slightly blurred. Both Magicks got much better text.
PHPixie itself breaks the text into lines, but for this it uses information about the text metric from the graphics library. Since you have to consider the size of each word, this can be a rather expensive process. So let's try to put a piece of the song on a blank page.
$text = "When I closed my eyes to the shimmering light ". "all memory faded and I could see ". "that a mushroom circle of red and white ". "and myriad fairies surrounded me. "; $pixie->image->create(300, 180, 0xffffff, 1) ->text($text, 20, $dir.'/Sofia-Regular.ttf', 10, 50, 0x5B43AA, 1, 300) ->save($dir.'wrap.jpg');
On a smaller font, the flaws are not so noticeable, but if you look closely at the bends, it can be quite easily distinguished. But it should be noted that Imagick spent a lot more time, primarily due to recounting the font metrics on each word (after all, it is necessary to read the font from the disk anew every time) while GD and Gmagick were much faster.
Drawing overlay and wrapping
This test is the most divorced from reality, but I wanted to benchmark the speed of overlaying drawings and, in general, more complicated operations. So we create an empty picture, on the one hand we put the fairy’s autark and on the other the same avatar only wrapped horizontally and vertically and even turned sideways.
$canvas = $pixie->image->create(400, 200); $fairy = $pixie->image->read($dir.'fairy.jpg') ->resize(200, 200, false) ->crop(200, 200); $canvas ->overlay($fairy, 0, 0) ->overlay($fairy->flip(true, true)->rotate(90), 200, 0) ->save($dir.'overlay.jpg');
And here is the benchmark itself. The tests performed horizontally from left to right, and the average execution time vertically (each test was run 100 times).
Conclusion, it is best to use Gmagick, if it is not, then Imagick. You can think about GD only if you need to write a lot of text and then with a not very intricate font.
By the way, I’ll separately note how much I liked the interface of PHPixie itself in this regard, especially the availability of functions for many standard actions (the same text wrapping) for which it was necessary to download individual classes before.