cvs: pear /Image_Transform/Drivers GD.php
| From: | Pierre-Alain Joye | Date: | Fri, 23 Aug 2002 12:00:01 +0000 |
| Subject: | cvs: pear /Image_Transform/Drivers GD.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5903@lists.php.net to get a copy of this message | ||
pajoye Fri Aug 23 08:00:01 2002 EDT
Modified files:
/pear/Image_Transform/Drivers GD.php
Log:
Fix bugs in display, addText
Added Rotate method for custom angle rotation
Index: pear/Image_Transform/Drivers/GD.php diff -u pear/Image_Transform/Drivers/GD.php:1.3 pear/Image_Transform/Drivers/GD.php:1.4 --- pear/Image_Transform/Drivers/GD.php:1.3 Mon Aug 19 14:41:29 2002 +++ pear/Image_Transform/Drivers/GD.php Fri Aug 23 08:00:00 2002 @@ -16,12 +16,25 @@ // | Alan Knowles <alan@akbkhome.com> | // +----------------------------------------------------------------------+ // -// $Id: GD.php,v 1.3 2002/08/19 18:41:29 reywob Exp $ +// Usage : +// $img = new Image_Transform_GD(); +// $angle = -78; +// $img->load('magick.png'); +// +// if($img->rotate($angle,array('autoresize'=>true,'color_mask'=>array(255,0,0)))){ +// $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'/usr/share/fonts/default/TrueType/cogb____.ttf')); +// $img->display(); +// } else { +// echo "Error"; +// } +// +// +// $Id: GD.php,v 1.4 2002/08/23 12:00:00 pajoye Exp $ // // Image Transformation interface using the GD library // -require_once "Image/Image_Transform.php"; +require_once "Image/Transform.php"; Class Image_Transform_GD extends Image_Transform { @@ -33,14 +46,14 @@ * Holds the original image file */ var $old_image = ''; - + /** - * Check settings + * Check settings * * @return mixed true or or a PEAR error object on error * * @see PEAR::isError() - */ + */ function Image_Transform_GD() { if (!function_exists("ImageTypes")) @@ -49,92 +62,261 @@ return PEAR::raiseError("No supported image types available", true); return; } // End function Image - + /** - * Load image + * Load image * * @param string filename * * @return mixed none or a PEAR error object on error * @see PEAR::isError() - */ + */ function load($image) { $this->image = $image; $this->_get_image_details($image); $functionName = 'ImageCreateFrom' . $this->type; $this->imageHandle = $functionName($this->image); - } // End load - + function addText($params) { $default_params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, - 'color' => 'red', + 'color' => array(255,0,0), 'font' => 'Arial.ttf', 'size' => '12', - 'angle' => 0; + 'angle' => 0, 'resize_first' => false // Carry out the scaling of the image before annotation? ); $params = array_merge($default_params, $params); extract($params); + + $c = imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]); + if ('ttf' == substr($font, -3)) { - ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); + ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } else { - ImagePSText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); + ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } + //imagestring($this->imageHandle,5,$x,$y,$text,$color); } // End addText - - function rotate($angle) + + + + /** + * Rotate image by the given angle + * Uses a fast rotation algorythm for custom angles + * or lines copy for multiple of 90 degrees + * + * @param int $angle Rotation angle + * @param array $options array( 'autoresize'=>true|false, + * 'color_mask'=>array(r,g,b) + * ) + * + * @return mixed none or a PEAR error object on error + * @see PEAR::isError() + */ + function rotate($angle, $options=null) { - if ('-' == $angle{0}) { - $angle = 360 - substr($angle, 1); - } - $size = GetImageSize($img_sorgente); - $tot_x = $this->img_x; - $tot_y = $this->img_y; - - $img_risulta = ImageCreate ($tot_y,$tot_x) - - $img_sorgente=ImageCreateFromJpeg($img_sorgente); - - for($i_x=0;$i_x<$tot_x;$i_x++){ - for($i_y=0;$i_y<$tot_y;$i_y++){ - $ris_x=$tot_y-($i_y+1); - $ris_y=$i_x; - imagecopy($img_risulta, $img_sorgente, $ris_x,$ris_y,$i_x,$i_y,1,1); - } // Y - } // X + if ( $options==null ){ + $autoresize = true; + $color_mask = array(255,255,0); + } else { + extract( $options ); + } + + while ($angle <= -45) { + $angle += 360; + } + while ($angle > 270) { + $angle -= 360; + } + + $t = deg2rad($angle); + + // Do not round it, too much lost of quality + $cosT = cos($t); + $sinT = sin($t); + + $img =& $this->imageHandle; + + $width = $max_x = $this->img_x; + $height = $max_y = $this->img_y; + $min_y = 0; + $min_x = 0; + + $x1 = round($max_x/2,0); + $y1 = round($max_y/2,0); + + if ( $autoresize ){ + $t = abs($t); + $a = round($angle,0); + switch((int)($angle)){ + case 0: + $width2 = $width; + $height2 = $height; + break; + case 90: + $width2 = $height; + $height2 = $width; + break; + case 180: + $width2 = $width; + $height2 = $height; + break; + case 270: + $width2 = $height; + $height2 = $width; + break; + default: + $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); + $height2 = (int)(abs(cos($t) * $height+sin($t) * $width)); + } + + $width2 -= $width2%2; + $height2 -= $height2%2; + + $d_width = abs($width - $width2); + $d_height = abs($height - $height2); + $x_offset = $d_width/2; + $y_offset = $d_height/2; + $min_x2 = -abs($x_offset); + $min_y2 = -abs($y_offset); + $max_x2 = $width2; + $max_y2 = $height2; + } + + $img2 = @imagecreate($width2,$height2); + + if ( !is_resource($img2) ){ + return PEAR::raiseError('Cannot create buffer for the rotataion.', + null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + + imagepalettecopy($img2,$img); + + $mask = imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]); + + // use simple lines copy for axes angles + switch((int)($angle)){ + case 0: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $min_x; $x < $max_x; $x++){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); + } + } + break; + case 90: + imagefill ($img2, 0, 0,$mask); + for ($x = $min_x; $x < $max_x; $x++){ + for ($y=$min_y; $y < $max_y; $y++) { + $c = imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$max_y-$y-1,$x,$c); + } + } + break; + case 180: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $min_x; $x < $max_x; $x++){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c); + } + } + break; + case 270: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $max_x; $x >= $min_x; $x--){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$y,$max_x-$x-1,$c); + } + } + break; + // simple reverse rotation algo + default: + $i=0; + for ($y = $min_y2; $y < $max_y2; $y++){ + + // Algebra :) + $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0); + $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0); + + for ($x = $min_x2; $x < $max_x2; $x++){ + + // Check if we are out of original bounces, if we are + // use the default color mask + if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){ + $c = imagecolorat ( $img, $x2, $y2); + } else { + $c = $mask; + } + imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); + + // round verboten! + $x2 += $cosT; + $y2 -= $sinT; + } + } + break; + } + $this->old_image = $this->imageHandle; + $this->imageHandle = $img2; + return true; } + + /** * Resize Action * + * For GD 2.01+ the new copyresampled function is used + * It uses a bicubic interpolation algorythm to get far + * better result. + * + * @param $new_x int new width + * @param $new_y int new height + * * @return none * @see PEAR::isError() */ function _resize($new_x, $new_y) { if ($this->resized === true) { - PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + return PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + if ( $new_img = ImageCreateTrueColor($new_x,$new_y) ){ + return PEAR::raiseError('Cannot create the resize buffer.', + null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + if(function_exists('ImageCopyResampled')){ + ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); + } else { + ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); } - $new_img = ImageCreateTrueColor($new_x,$new_y); - ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); $this->old_image = $this->imageHandle; $this->imageHandle = $new_img; $this->resized = true; - + $this->new_x = $new_x; - $this->new_y = $new_y; + $this->new_y = $new_y; + return true; } - + + /** * Save the image file - * - * @param $filename string the name of the file to write to + * + * @param $filename string the name of the file to write to + * @param $quality int output DPI, default is 75 + * @param $types string define the output format, default + * is the current used format + * * @return none */ function save($filename, $quality = 75, $type = '') @@ -165,8 +347,10 @@ $functionName($this->imageHandle, '', $quality); $this->imageHandle = $this->old_image; $this->resized = false; + ImageDestroy($this->old_image); + $this->free(); } - + /** * Destroy image handle * @@ -178,6 +362,6 @@ ImageDestroy($this->imageHandle); } } - -} // End class ImageIM + +} // End class ImageGD ?>
Index: pear/Image_Transform/Drivers/GD.php diff -u pear/Image_Transform/Drivers/GD.php:1.3 pear/Image_Transform/Drivers/GD.php:1.4 --- pear/Image_Transform/Drivers/GD.php:1.3 Mon Aug 19 14:41:29 2002 +++ pear/Image_Transform/Drivers/GD.php Fri Aug 23 08:00:00 2002 @@ -16,12 +16,25 @@ // | Alan Knowles <alan@akbkhome.com> | // +----------------------------------------------------------------------+ // -// $Id: GD.php,v 1.3 2002/08/19 18:41:29 reywob Exp $ +// Usage : +// $img = new Image_Transform_GD(); +// $angle = -78; +// $img->load('magick.png'); +// +// if($img->rotate($angle,array('autoresize'=>true,'color_mask'=>array(255,0,0)))){ +// $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'/usr/share/fonts/default/TrueType/cogb____.ttf')); +// $img->display(); +// } else { +// echo "Error"; +// } +// +// +// $Id: GD.php,v 1.4 2002/08/23 12:00:00 pajoye Exp $ // // Image Transformation interface using the GD library // -require_once "Image/Image_Transform.php"; +require_once "Image/Transform.php"; Class Image_Transform_GD extends Image_Transform { @@ -33,14 +46,14 @@ * Holds the original image file */ var $old_image = ''; - + /** - * Check settings + * Check settings * * @return mixed true or or a PEAR error object on error * * @see PEAR::isError() - */ + */ function Image_Transform_GD() { if (!function_exists("ImageTypes")) @@ -49,92 +62,261 @@ return PEAR::raiseError("No supported image types available", true); return; } // End function Image - + /** - * Load image + * Load image * * @param string filename * * @return mixed none or a PEAR error object on error * @see PEAR::isError() - */ + */ function load($image) { $this->image = $image; $this->_get_image_details($image); $functionName = 'ImageCreateFrom' . $this->type; $this->imageHandle = $functionName($this->image); - } // End load - + function addText($params) { $default_params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, - 'color' => 'red', + 'color' => array(255,0,0), 'font' => 'Arial.ttf', 'size' => '12', - 'angle' => 0; + 'angle' => 0, 'resize_first' => false // Carry out the scaling of the image before annotation? ); $params = array_merge($default_params, $params); extract($params); + + $c = imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]); + if ('ttf' == substr($font, -3)) { - ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); + ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } else { - ImagePSText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); + ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } + //imagestring($this->imageHandle,5,$x,$y,$text,$color); } // End addText - - function rotate($angle) + + + + /** + * Rotate image by the given angle + * Uses a fast rotation algorythm for custom angles + * or lines copy for multiple of 90 degrees + * + * @param int $angle Rotation angle + * @param array $options array( 'autoresize'=>true|false, + * 'color_mask'=>array(r,g,b) + * ) + * + * @return mixed none or a PEAR error object on error + * @see PEAR::isError() + */ + function rotate($angle, $options=null) { - if ('-' == $angle{0}) { - $angle = 360 - substr($angle, 1); - } - $size = GetImageSize($img_sorgente); - $tot_x = $this->img_x; - $tot_y = $this->img_y; - - $img_risulta = ImageCreate ($tot_y,$tot_x) - - $img_sorgente=ImageCreateFromJpeg($img_sorgente); - - for($i_x=0;$i_x<$tot_x;$i_x++){ - for($i_y=0;$i_y<$tot_y;$i_y++){ - $ris_x=$tot_y-($i_y+1); - $ris_y=$i_x; - imagecopy($img_risulta, $img_sorgente, $ris_x,$ris_y,$i_x,$i_y,1,1); - } // Y - } // X + if ( $options==null ){ + $autoresize = true; + $color_mask = array(255,255,0); + } else { + extract( $options ); + } + + while ($angle <= -45) { + $angle += 360; + } + while ($angle > 270) { + $angle -= 360; + } + + $t = deg2rad($angle); + + // Do not round it, too much lost of quality + $cosT = cos($t); + $sinT = sin($t); + + $img =& $this->imageHandle; + + $width = $max_x = $this->img_x; + $height = $max_y = $this->img_y; + $min_y = 0; + $min_x = 0; + + $x1 = round($max_x/2,0); + $y1 = round($max_y/2,0); + + if ( $autoresize ){ + $t = abs($t); + $a = round($angle,0); + switch((int)($angle)){ + case 0: + $width2 = $width; + $height2 = $height; + break; + case 90: + $width2 = $height; + $height2 = $width; + break; + case 180: + $width2 = $width; + $height2 = $height; + break; + case 270: + $width2 = $height; + $height2 = $width; + break; + default: + $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); + $height2 = (int)(abs(cos($t) * $height+sin($t) * $width)); + } + + $width2 -= $width2%2; + $height2 -= $height2%2; + + $d_width = abs($width - $width2); + $d_height = abs($height - $height2); + $x_offset = $d_width/2; + $y_offset = $d_height/2; + $min_x2 = -abs($x_offset); + $min_y2 = -abs($y_offset); + $max_x2 = $width2; + $max_y2 = $height2; + } + + $img2 = @imagecreate($width2,$height2); + + if ( !is_resource($img2) ){ + return PEAR::raiseError('Cannot create buffer for the rotataion.', + null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + + imagepalettecopy($img2,$img); + + $mask = imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]); + + // use simple lines copy for axes angles + switch((int)($angle)){ + case 0: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $min_x; $x < $max_x; $x++){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); + } + } + break; + case 90: + imagefill ($img2, 0, 0,$mask); + for ($x = $min_x; $x < $max_x; $x++){ + for ($y=$min_y; $y < $max_y; $y++) { + $c = imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$max_y-$y-1,$x,$c); + } + } + break; + case 180: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $min_x; $x < $max_x; $x++){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c); + } + } + break; + case 270: + imagefill ($img2, 0, 0,$mask); + for ($y=0; $y < $max_y; $y++) { + for ($x = $max_x; $x >= $min_x; $x--){ + $c = @imagecolorat ( $img, $x, $y); + imagesetpixel($img2,$y,$max_x-$x-1,$c); + } + } + break; + // simple reverse rotation algo + default: + $i=0; + for ($y = $min_y2; $y < $max_y2; $y++){ + + // Algebra :) + $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0); + $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0); + + for ($x = $min_x2; $x < $max_x2; $x++){ + + // Check if we are out of original bounces, if we are + // use the default color mask + if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){ + $c = imagecolorat ( $img, $x2, $y2); + } else { + $c = $mask; + } + imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); + + // round verboten! + $x2 += $cosT; + $y2 -= $sinT; + } + } + break; + } + $this->old_image = $this->imageHandle; + $this->imageHandle = $img2; + return true; } + + /** * Resize Action * + * For GD 2.01+ the new copyresampled function is used + * It uses a bicubic interpolation algorythm to get far + * better result. + * + * @param $new_x int new width + * @param $new_y int new height + * * @return none * @see PEAR::isError() */ function _resize($new_x, $new_y) { if ($this->resized === true) { - PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + return PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + if ( $new_img = ImageCreateTrueColor($new_x,$new_y) ){ + return PEAR::raiseError('Cannot create the resize buffer.', + null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); + } + if(function_exists('ImageCopyResampled')){ + ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); + } else { + ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); } - $new_img = ImageCreateTrueColor($new_x,$new_y); - ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); $this->old_image = $this->imageHandle; $this->imageHandle = $new_img; $this->resized = true; - + $this->new_x = $new_x; - $this->new_y = $new_y; + $this->new_y = $new_y; + return true; } - + + /** * Save the image file - * - * @param $filename string the name of the file to write to + * + * @param $filename string the name of the file to write to + * @param $quality int output DPI, default is 75 + * @param $types string define the output format, default + * is the current used format + * * @return none */ function save($filename, $quality = 75, $type = '') @@ -165,8 +347,10 @@ $functionName($this->imageHandle, '', $quality); $this->imageHandle = $this->old_image; $this->resized = false; + ImageDestroy($this->old_image); + $this->free(); } - + /** * Destroy image handle * @@ -178,6 +362,6 @@ ImageDestroy($this->imageHandle); } } - -} // End class ImageIM + +} // End class ImageGD ?>