note 104554 added to function.imagecolortransparent
| From: | email at vladislav dot net | Date: | Wed, 22 Jun 2011 21:42:08 +0000 |
| Subject: | note 104554 added to function.imagecolortransparent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180852@lists.php.net to get a copy of this message | ||
Pay attention, that some GIF images may not include a transparent color. A good example of forced
transparency in resized GIF image was given by markglibres at yahoo dot com 29-Mar-2009 02:48. But
sometimes the transparent color in GIF images can be not set. The problem is, that the color you
force to be transparent can be used in the original GIF as opaque and you will loose that color in
resized image. The solution is not to use some default transparent color and to leave the resized
image without transparent color (the same as original GIF). I used (nearly) the following code to
make resized GIF images trnsparent only when the transparency is needed:
<?php
/* ... */
$img = ImageCreateFromGIF($f); /* create image from existing GIF: original image file name $f you
may take from where you need */
$transparent_index = ImageColorTransparent($img); /* gives the index of current transparent color or
-1 */
if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);
/* ... */
$img_resized = ImageCreateTrueColor( $nw, $nh ); /* (the new width $nw and height $nh must be
defined before) */
if(!empty($transparent_color)) /* simple check to find wether transparent color was set or not */
{
$transparent_new = ImageColorAllocate( $img_resized, $transparent_color['red'],
$transparent_color['green'], $transparent_color['blue'] );
$transparent_new_index = ImageColorTransparent( $img_resized, $transparent_new );
ImageFill( $img_resized, 0,0, $transparent_new_index ); /* don't forget to fill the new image
with the transparent color */
}
list($w,$h) = GetImageSize($f); /* defines the original width $w and height $h */
if( ImageCopyResized( $img_resized, $img, 0,0, 0,0, $nw,$nh, $w,$h ) ) /* resized copying and
replacing the original image */
{
ImageDestroy($img);
$img = $img_resized;
}
/* ... */
header('Content-Type: image/gif');
ImageGIF($img);
ImageDestroy($img);
?>
P.S.
If there some errors, I hope you could understood the idea.
----
Server IP: 89.251.21.32
Probable Submitter: 91.124.217.223
----
Manual Page -- http://www.php.net/manual/en/function.imagecolortransparent.php
Edit -- https://master.php.net/note/edit/104554
Del: integrated -- https://master.php.net/note/delete/104554/integrated
Del: useless -- https://master.php.net/note/delete/104554/useless
Del: bad code -- https://master.php.net/note/delete/104554/bad+code
Del: spam -- https://master.php.net/note/delete/104554/spam
Del: non-english -- https://master.php.net/note/delete/104554/non-english
Del: in docs -- https://master.php.net/note/delete/104554/in+docs
Del: other reasons-- https://master.php.net/note/delete/104554
Reject -- https://master.php.net/note/reject/104554
Search -- https://master.php.net/manage/user-notes.php