📜 ⬆️ ⬇️

Benchmark of graphic libraries for PHP

image
In the project that we are currently developing a lot of work with photos, as a result, the speed of the graphic library is actually the main factor in optimizing the site. It's no secret that ImageMagick has long become a standard for developing in PHP, although for some time it has been pushed from there by a fork called GraphicsMagick. I wondered how much they differ and how much the odds can give the good old GD. Fortunately, just recently a graphics module for PHPixie was released which 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 http://j-scott-campbell.deviantart.com/ ). If it seems to someone that there is too much innuendo in it, then I dare to remind that for more than 20 years Lenna from the playboy is the standard pattern of testing of graphical 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 work in one table.
')
Simple scaling

So first try to reduce it to 100 pixels in width.
$pixie->image->read($dir.'fairy.jpg') ->resize(200) ->save($dir.'resize.jpg'); 


image

The difference is not noticeable at first glance, but for example its eye on the GD version does not have that pronounced green color like Imagick and Gmagick.

Create square avatars

To make an autark square it must first be scaled on the smaller side and then cut off on the larger side. At PHPixie, this is done quite simply:

 $pixie->image->read($dir.'fairy.jpg') ->resize(200, 200, false) ->crop(200, 200) ->save($dir.'crop.jpg'); 

image
It is obvious that they will look like the previous one.

Writing text and creating a blank image

Now we will try to create a white drawing 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'); 


image

Here the results are slightly more dramatic. GD for some reason made the text color darker and anti-aliasing worked much worse, with the result that the edges of the text are slightly blurred. Both Magick's text is much better.

Splitting text into lines

The text itself is broken into lines by PHPixie, but for this it uses information about the text metrics from the graphics library. Since you have to consider the size of each word, this can be a rather expensive process. So try to put on a blank page a piece of a song.
 $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'); 


image

On a smaller font, the flaws are not so noticeable, but if you look at the bends, you can quite easily distinguish them. But it should be noted that Imagick spent much more time, primarily because of recalculating the font metrics on each word (after all, you have to read the font again from the disk each time) while GD and Gmagick were much faster.

Overlay and wrapping pictures

This test is the most disconnected from reality, but I wanted to beat the speed of overlaying pictures and, in general, the operation was more complicated. So, we create an empty picture, on the one hand we put the fairy autarka and on the other side the same avatar only wrapped horizontally and vertically and also unfolded 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'); 


image

Work speed


And here is the benchmark itself. Horizontal left-to-right tests, vertical average execution time (each test was run 100 times).

image

Conclusion, it’s best to use Gmagick if it’s not there, then Imagick. You can think about GD only if you need to write a lot of text and that is not a very intricate font.

By the way, I’ll separately note how much I liked the interface of PHPixie itself in this regard, especially the presence of functions for a variety of standard actions (the same text wrap) for which previously had to download separate classes.

Source: https://habr.com/ru/post/188666/


All Articles