note 92794 modified in function.imagerotate by danbrown
| From: | danbrown@php.net | Date: | Sun, 09 Aug 2009 17:41:49 +0000 |
| Subject: | note 92794 modified in function.imagerotate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159241@lists.php.net to get a copy of this message | ||
I wanted to draw a transparent GIF-image and show it on a page, at an angle specified in the URL:
<img src="image.php?angle=90" type="image/gif">
I used the native imagerotate() but at angles of 90, 180, etc. the old background colour would
become non-transparent. Apparently, there's a bug somewhere in GD, and has been for ages.
My solution below:
<?php
$height = 100;
$width = 100;
$lsize= $width/2;
$angle= $_GET["angle"];
// avoid the bug:
if(($angle%90)==0)
$angle+= 0.001;
$image_p = imagecreatetruecolor($width, $height);
$trans = imagecolorallocate($image_p, 254, 0, 0);
imagefill($image_p, 0, 0, $trans);
imagecolortransparent($image_p, $trans);
$black = imagecolorallocate($image_p, 1, 1, 1);
$red = imagecolorallocate($image_p, 255, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
// draw something here
imageline($image_p, 3, $lsize, $lsize/2, $lsize, $black);
$image_r= imagerotate($image_p, -$angle, $trans, 0);
$w= imagesx($image_r);
$h= imagesy($image_r);
$image_s = imagecreatetruecolor($width, $height);
imagecopyresized($image_s, $image_r, 0, 0, ($w-$width)/2, ($h-$height)/2, $width, $height, $width,
$height);
$trans = imagecolorallocate($image_s, 254, 0, 0);
imagecolortransparent($image_s, $trans);
imagegif($image_s);
?>
--was--
I wanted to draw a transparent GIF-image and show it on a page, at an angle specified in the URL:
<img src="image.php?angle=90" type="image/gif">
I used the native imagerotate() but at angles of 90, 180, etc. the old background colour would
become non-transparent. Apparently, there's a bug somewhere in GD, and has been for ages.
My solution below:
$height = 100;
$width = 100;
$lsize= $width/2;
$angle= $_GET["angle"];
// avoid the bug:
if(($angle%90)==0)
$angle+= 0.001;
$image_p = imagecreatetruecolor($width, $height);
$trans = imagecolorallocate($image_p, 254, 0, 0);
imagefill($image_p, 0, 0, $trans);
imagecolortransparent($image_p, $trans);
$black = imagecolorallocate($image_p, 1, 1, 1);
$red = imagecolorallocate($image_p, 255, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
// draw something here
imageline($image_p, 3, $lsize, $lsize/2, $lsize, $black);
$image_r= imagerotate($image_p, -$angle, $trans, 0);
$w= imagesx($image_r);
$h= imagesy($image_r);
$image_s = imagecreatetruecolor($width, $height);
imagecopyresized($image_s, $image_r, 0, 0, ($w-$width)/2, ($h-$height)/2, $width, $height, $width,
$height);
$trans = imagecolorallocate($image_s, 254, 0, 0);
imagecolortransparent($image_s, $trans);
imagegif($image_s);
http://php.net/manual/en/function.imagerotate.php