note 46338 modified in function.imagerotate by danbrown

From: Date: Tue, 02 Dec 2008 15:02:15 +0000
Subject: note 46338 modified in function.imagerotate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147544@lists.php.net to get a copy of this message
imagerotate does not preserve the alpha channel, so if you want to rotate a PNG you need to get creative. I don't see any command to retrieve the alpha information from an image (as far as I could see,) so you'll have to do a bit of manual labor before hand. In my case I created a second PNG file with the alpha saved as RGB data and manually "copied" the data from source to destination: <?php function alpha_rotate($dst,$src,$rotate,$offsetX,$offsetY){ $top = imagecreatefrompng("image_processing/shadow.png"); $top_alpha = imagecreatefrompng("image_processing/shadow_alpha.png"); imagecopyresampled($top,$src,0,0,0,0,100,100,100,100); $top = imagerotate($top,$rotate,0x000000); $top_alpha = imagerotate($top_alpha,$rotate,0x000000); for ($theX=0;$theX<imagesx($top);$theX++){ for ($theY=0;$theY<imagesy($top);$theY++){ $rgb = imagecolorat($top,$theX,$theY); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $rgb = imagecolorat($top_alpha,$theX,$theY); $a = $rgb & 0xFF; $a = 127-floor($a/2); $myColor = imagecolorallocatealpha($top,$r,$g,$b,$a); imagesetpixel($dst,($theX+$offsetX),($theY+$offsetY),$myColor); } } } ?> --was-- imagerotate does not preserve the alpha channel, so if you want to rotate a PNG you need to get creative. I don't see any command to retrieve the alpha information from an image (as far as I could see,) so you'll have to do a bit of manual labor before hand. In my case I created a second PNG file with the alpha saved as RGB data and manually "copied" the data from source to destination: function alpha_rotate($dst,$src,$rotate,$offsetX,$offsetY){ $top = imagecreatefrompng("image_processing/shadow.png"); $top_alpha = imagecreatefrompng("image_processing/shadow_alpha.png"); imagecopyresampled($top,$src,0,0,0,0,100,100,100,100); $top = imagerotate($top,$rotate,0x000000); $top_alpha = imagerotate($top_alpha,$rotate,0x000000); for ($theX=0;$theX<imagesx($top);$theX++){ for ($theY=0;$theY<imagesy($top);$theY++){ $rgb = imagecolorat($top,$theX,$theY); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $rgb = imagecolorat($top_alpha,$theX,$theY); $a = $rgb & 0xFF; $a = 127-floor($a/2); $myColor = imagecolorallocatealpha($top,$r,$g,$b,$a); imagesetpixel($dst,($theX+$offsetX),($theY+$offsetY),$myColor); } } } http://php.net/manual/en/function.imagerotate.php

« previous php.notes (#147544) next »