note 99591 added to function.imagerotate

From: Date: Wed, 25 Aug 2010 15:38:52 +0000
Subject: note 99591 added to function.imagerotate
Groups: php.notes 
Request: Send a blank email to php-notes+get-172060@lists.php.net to get a copy of this message
I've benchmarked three methods described here for rotating an image. I used a 1600 x 1200 jpeg picture that was duplicated ten times on the disk. I looped through them to apply a CW rotation using either method. First algorithm (the "imagesetpixel" algorithm): <?php function CWRotation($image) { $w = imagesx($image); $h = imagesy($image); $result = @imagecreatetruecolor($h, $w); if($result) { for ($i = 0; $i < $w; $i++) for ($j = 0; $j < $h; $j++) { $ref = imagecolorat($image, $i, $j); imagesetpixel($result, ($h - 1) - $j, $i, $ref); } } return $result; } ?> Second algorithm (the "imagecopy" algorithm): <?php function CWRotation($image) { $w = imagesx($image); $h = imagesy($image); $result = @imagecreatetruecolor($h, $w); if($result) { for ($i = 0; $i < $w; $i++) for ($j = 0; $j < $h; $j++) imagecopy($result, $image, ($h - 1) - $j, $i, $i, $j, 1, 1); } return $result; } ?> Third algorithm (the "imagerotate" algorithm): <?php function CWRotation($image) { return imagerotate($image, 270, 0); } ?> In each step of the loop, the image is loaded, rotated then a copy is written on the disk. Here are the results: imagesetpixel algorithm: the 10 pictures were processed in 29 seconds imagecopy algorithm: the 10 pictures were processed in 26 seconds imagerotate algorithm: the 10 pictures were processed in 2 seconds (no typo here) Writing the pictures on the disk allowed me to control the results. Every resulting picture was the same (tested with winmerge). Conclusion: imagerotate is way faster than other "home-made" algorithms. I ran the tests several times to see if the difference between imagesetpixel and imagecopy was introduced by my computer's independent activity, but I always got that 10% difference. Note: the picture I used was taken from a camera, so no transparency was involved. I ran the tests on Windows XP using PHP Version 5.3.0 ---- Server IP: 195.221.21.36 Probable Submitter: 192.54.144.229 ---- Manual Page -- http://www.php.net/manual/en/function.imagerotate.php Edit -- https://master.php.net/note/edit/99591 Del: integrated -- https://master.php.net/note/delete/99591/integrated Del: useless -- https://master.php.net/note/delete/99591/useless Del: bad code -- https://master.php.net/note/delete/99591/bad+code Del: spam -- https://master.php.net/note/delete/99591/spam Del: non-english -- https://master.php.net/note/delete/99591/non-english Del: in docs -- https://master.php.net/note/delete/99591/in+docs Del: other reasons-- https://master.php.net/note/delete/99591 Reject -- https://master.php.net/note/reject/99591 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172060) next »