Kore Nordmann - PHP / Projects / Politics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Kore Nordmann :Date: Thu, 20 Dec 2007 17:46:24 +0100 :Revision: 3 :Copyright: CC by-sa ================================== Comparision of PHP image libraries ================================== :Description: A comparision of speed and output quality of PHP libraries for image creation. I find this always a bit hard to phrase, because I am not only comparing libraries here, but also formats or extensions which are just used to create some graphic, which also may be used for something completely different (DOM). I am talking about general ways to create graphics with PHP here. In my article series I showed several ways on how to create images with PHP, going into implementation details etc. Now I just want to provide a very rough overview on speed and quality of the extensions. :: * kn::Graphic::Cairo (250): 0.0651s (0.000260) * kn::Graphic::Svg (250): 0.0973s (0.000389) * kn::Graphic::Flash (250): 0.1271s (0.000509) * kn::Graphic::GdWithoutSupersampling (250): 0.1926s (0.000770) * kn::Graphic::Gd (250): 0.5291s (0.002116) .. image:: bench_cairo.png :width: 150px :height: 150px :alt: Cairo output :align: right For this microbenchmark I disabled stuff like the border size reduction, which would cost some additional time for SVG and Flash generation, but I am not really sure if you would always need that. .. image:: bench_gd.png :width: 150px :height: 150px :alt: GD output :align: right You can see that Cairo is a lot faster then everything else, even with disabled supersampling in GD. Flash and SVG are actually rendered by the Client (the visiting browser or similar), so it is not completely fair comparision. .. image:: bench_gd_nos.png :width: 150px :height: 150px :alt: GD no supersampling :align: right Beside the speed there is another thing to mention about Cairo: It produces the output with the highest quality. Yeah, it is faster then everything else, and the created images are of the best quality. From my point of view it should be the default for image creation with PHP - and you should use it at least, when you are able to install a pecl extension. Here are some references, if you want to get more detailed information: - `Image creation with PHP`__ - `pecl/cairo_wrapper`__ by `Hartmut Holzgraefe`__ - `Supersampling with GD`__ - `Border size reduction`__ __ /blog/image_creation_with_php_formats.html __ /blog/image_creation_with_php_formats.html#id39 __ http://www.php-groupies.de/blogs/ __ /blog/image_creation_with_php_shapes.html#id26 __ /blog/image_creation_with_php_shapes.html#id34 The benchmark code ------------------ Here is the benchmark code. It does a lot calculations on the PHP side, using the transformation matrix to rotate the polygon, so that it should be somehow like a usual real world application. If you remaove that, you would get an even bigger speed gain from Cairo. :: addBitmap( new Coordinate( 50, 129 ), 'bitmap.png' ); $polygon = new Polygon( new Coordinate( 75, 75 ), new Coordinate( 75, 10 ), new Coordinate( 85, 30 ) ); $transformation = Matrix::createRotationMatrixAroundPoint( -10, new Coordinate( 75, 75 ) ); for ( $i = 0; $i < 360; $i += 10 ) { $graphic->drawPolygon( $polygon, new Color( '#2e35367f' ), true ); $graphic->drawPolygon( $polygon, new Color( '#eeeeef7f' ), false ); $polygon->transform( $transformation ); } $graphic->save( $class ); // Free memory unset( $graphic, $polygon, $transformation ); } // Echo used time printf( "% 40s (%d): %02.4fs (%1.6f)\n", $class, $iterations, $time = ( microtime(true) - $start ), $time / $iterations ); } The abstraction layer I use here is the one described in my article series on `"Image creation with PHP"`__, without the border size reduction, as mentioned above. __ /blog/image_creation_with_php_formats.html Update ------ I posted an update__ of this comparision including iMagick. __ comparison_of_php_image_libraries_update.html Trackbacks ========== Comments ======== - Alexey Zakhlestin at Wed, 12 Dec 2007 15:34:21 +0100 What about these? http://magickwand.org/ http://pecl.php.net/package/imagick - kore at Wed, 12 Dec 2007 17:33:40 +0100 imagemagick (which is behind both mentioned extensions) is more for image manipulation, and not for image creation. Some of the things required in the described abstraction layer can't be easily implemented with them. So, I skipped them, because they have a different focus, and Mikko describes iMagick very well in his blog: http://valokuva.org/?cat=1 - BVis at Wed, 12 Dec 2007 23:58:56 +0100 Would be very interesting include in the comparison the result with the new Imagick2 PHP Library... ;) - open source cms at Thu, 13 Dec 2007 22:40:06 +0100 @BVis: I will wait for this results :)