note 62241 added to function.imagefilledellipse

From: Date: Thu, 23 Feb 2006 03:33:56 +0000
Subject: note 62241 added to function.imagefilledellipse
Groups: php.notes 
Request: Send a blank email to php-notes+get-104262@lists.php.net to get a copy of this message
This is a niffty function that you can use to make transparent ellipse/round type cutouts of any PNG or GIF image. The hard part is finding what color to use for the cutout layer and then the transparent layer, because you don't want to set the transparent index to a color being used in the image. After that it's as simple as layering the two images together. what you need for the example... a image of the size you want the cutout to be, gif or png (true color /256) can be used! <? $original_image = './image.png'; $output_image = './new.png'; $temp_image = './temp'; // path and name (don't include the extension) $is_true_color = true; $ext = substr ( $original_image, strrpos ( $original_image, '.' ) ); $temp_image .= $ext; $new = image_get ( $ext, $original_image ); $width = imagesx ( $new ); $height = imagesy ( $new ); // we need to create temp reduced image so we can get the colors // in a high bit true color image (png 16,24 bit only) if ( $is_true_color ) { imagetruecolortopalette ( $new, false, 256 ); image_make ( $new, $ext, $temp_image ); imagedestroy ( $new ); $colors = get_rgb ( $temp_image, $ext ); @unlink ( $temp_image ); $new = image_get ( $ext, $original_image ); } else { $colors = get_rgb ( $original_image, $ext ); } // this creates the cutout layer (2 colors, both will become transparent) $old = imagecreate ( $width, $height ); imageantialias( $old, true ); imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] ); $bg = imagecolorallocate ( $old, $colors[1]['red'], $colors[1]['green'], $colors[1]['blue'] ); imagefilledellipse ( $old, floor ( $width / 2 ), floor ( $height / 2 ), $width, $height, $bg ); imagecolortransparent ( $old, $bg ); imagecopy ( $new, $old, 0, 0, 0, 0, $width, $height ); image_make ( $new, $ext, $output_image ); imagedestroy ( $old ); imagedestroy ( $new ); // this layers both images together, making a nice ellipse/round transparent image cutout $old = imagecreate ( $width, $height ); $new = image_get ( $ext, $output_image ); $tbg = imagecolorallocate ( $old, $colors[0]['red'], $colors[0]['green'], $colors[0]['blue'] ); imagecopy ( $old, $new, 0, 0, 0, 0, $width, $height ); imagecolortransparent ( $old, $tbg ); image_make ( $old, $ext, $output_image ); imagedestroy ( $old ); imagedestroy ( $new ); /* * shortcut functions (1,2) */ // returns the called image resource function image_get ( $ext, $name ) { switch ( $ext ) { case '.gif' : return ( imagecreatefromgif ( $name ) ); break; case '.png' : return ( imagecreatefrompng ( $name ) ); break; } } // outputs the image named passed to it function image_make ( $io, $ext, $name ) { switch ( $ext ) { case '.gif' : imagegif ( $io, $name ); break; case '.png' : imagepng ( $io, $name ); break; } } // get (2) colors not found in the current image function get_rgb ( $image, $ext ) { $x = 0; $colors = array (); $img = image_get ( $ext, $image ); for ( $color = 10; $color <= 250; $color++ ) { if ( imagecolorexact ( $img, $color, $color, $color ) == -1 ) { $colors[] = array ( 'red' => $color, 'green' => $color, 'blue' => $color ); if ( $x == 1 ) { imagedestroy ( $img ); return ( $colors ); } $x++; } } imagedestory ( $img ); return ( $colors ); } ?> You can try a demo here (SGML) capture a web page, then make multi cutouts example! http://www.ya-right.com/ ---- Server IP: 216.194.113.175 Probable Submitter: 66.31.242.148 ---- Manual Page -- http://www.php.net/manual/en/function.imagefilledellipse.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62241 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62241&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62241&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104262) next »