note 42239 added to function.imagecopy
| From: | RT at rn2 dot php dot net | Date: | Sun, 09 May 2004 07:09:35 +0000 |
| Subject: | note 42239 added to function.imagecopy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69370@lists.php.net to get a copy of this message | ||
Although the following function doesn't use imagecopy(), I thought it might help in related
tasks. Please see the code comments for details of it's operation. I made this function to
assist in creating images using multiple "layers". For example if you wanted to
dynamically create a logo image with seperate colors for say the logo itself and a glow around the
logo, these steps would be followed:
-Using an image editor (like Photoshop), create a png-24 image with just the logo on a transparent
background. The logo can be any color or multiple colors, but the final image created by this
function will be of a single color.
-Create a similar image with just the glow (no logo)
-Create a background image
-Apply this colorize() function to the logo image and the glow image with your desired color for
each.
-You can now use imagecopy() to merge all three into a single image ready for a browser.
Here's the code
<?php
/*======Colorize=====
(requires GD 2.0.1 or greater)
This function requires the following arguments:
$src_path = A string representing the relative path to the src image. Ex:
"images/myimage.png". This
image must be a png-24 with an alpha channel.
$dest_path = A string representing the relative path to the image to be created.
$hex_color = A string representing a color in html format, including the # sign. Ex:
"#D2E5FF"
This function examines the transparency of the source image, pixel by pixel, and creates a new
one-color image with this same "transparency map".
====================*/
function colorize($src_path, $dest_path, $hex_color) {
//get the png-24 image - it must have an alpha channel for this funciton to be effective
$src = imagecreatefrompng($src_path);
//get width
$w = imagesx($src);
//get height
$h = imagesy($src);
//create same size destination image
$dest = imagecreatetruecolor($w, $h);
//this must be set to false in order to be able to overwright the defualt black pixels of the
background with our new
//transparent pixels. Otherwise our new pixel would just be applied on top of the black.
imagealphablending($dest, false);
//get decimal components of the passed hex color
$red = hexdec(substr($hex_color, 1, 2));
$green = hexdec(substr($hex_color, 3, 2));
$blue = hexdec(substr($hex_color, 5, 2));
for ($i = 0; $i < $h; $i++) { //this loop traverses each row in the image
for ($j = 0; $j < $w; $j++) { //this loop traverses each pixel of each row
//get the color & alpha info of the current pixel
$retrieved_color = imagecolorat($src, $j, $i);
//put this info into an array
$rgba_array = imagecolorsforindex($src, $retrieved_color);
//get the transparency of the pixel as a number from 0 (opaque) to 127 (transparent)
$alpha = $rgba_array['alpha'];
//allocate the color to paint. Note that we may continue to overwright this color since our image
is not palleted
$color_to_paint = imagecolorallocatealpha($dest, $red, $green, $blue, $alpha);
//paint the pixel
imagesetpixel($dest, $j, $i, $color_to_paint);
}
}
//this allows the new transparency info to be saved with the image
imagesavealpha($dest, true);
//write the image to the destination file
imagepng($dest, $dest_path);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecopy.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42239
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42239&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42239&report=yes
Search -- http://master.php.net/manage/user-notes.php