Comparision of PHP image libraries

First published at Wednesday, 12 December 2007

Warning: This blog post is more then 17 years old – read and use with care.

Comparision of PHP image libraries

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)
Cairo outputCairo output

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.

GD outputGD output

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.

GD no supersamplingGD no supersampling

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:

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.

<?php namespace kn::Graphic; require_once dirname( __FILE__ ) . '/../base.php'; // Define a GD class wrapper without supersampling class GdWithoutSupersampling extends GD { public function __construct( $width, $height ) { parent::__construct( $width, $height, 1 ); } } $iterations = 250; $classes = array( 'Cairo', 'Svg', 'Flash', 'Gd', 'GdWithoutSupersampling', ); foreach ( $classes as $class ) { // Store start time $start = microtime( true ); // Prefix class name with local namespace $class = __NAMESPACE__ . '::' . $class; // Create graphics for ( $i = 0; $i < $iterations; ++$i ) { $graphic = new $class( 150, 150 ); $graphic->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.

Update

I posted an update of this comparision including iMagick.


Comments

Alexey Zakhlestin at Wednesday, 12.12. 2007

What about these?

http://magickwand.org/ http://pecl.php.net/package/imagick

kore at Wednesday, 12.12. 2007

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 Wednesday, 12.12. 2007

Would be very interesting include in the comparison the result with the new Imagick2 PHP Library... ;)

open source cms at Thursday, 13.12. 2007

@BVis: I will wait for this results :)

Subscribe to updates

There are multiple ways to stay updated with new posts on my blog: