|
|
 |
imagecopymergegray (PHP 4 >= 4.0.6, PHP 5) imagecopymergegray -- Copy and merge part of an image with gray scale Descriptionint imagecopymergegray ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct )
imagecopymergegray() copy a part of src_im onto
dst_im starting at the x,y coordinates
src_x, src_y with
a width of src_w and a height of
src_h. The portion defined will be copied
onto the x,y coordinates, dst_x and
dst_y.
The two images will be merged according to pct
which can range from 0 to 100. When pct = 0,
no action is taken, when 100 this function behaves identically
to imagecopy().
This function is identical to imagecopymerge() except
that when merging it preserves the hue of the source by converting
the destination pixels to gray scale before the copy operation.
Note: This function was added in PHP 4.0.6
User Contributed Notes
imagecopymergegray
switch251 at netcourrier dot com
15-Apr-2004 09:18
In addition to code_couturier too: his code will produce blue pictures, because the value he uses to set the pixel color (the code is incomplete: I first thought it should be $gray) is between 0 and 255, which corresponds to blue levels.
To convert the picture to grayscale, use the following code:
<?php
$originalFileName = "colorPicture.jpg";
$destinationFileName = "bwPicture.jpg";
$fullPath = explode(".",$originalFileName);
$lastIndex = sizeof($fullPath) - 1;
$extension = $fullPath[$lastIndex];
if (preg_match("/jpg|jpeg|JPG|JPEG/", $extension)){
$sourceImage = imagecreatefromjpeg($originalFileName);
}
$img_width = imageSX($sourceImage);
$img_height = imageSY($sourceImage);
for ($y = 0; $y <$img_height; $y++) {
for ($x = 0; $x <$img_width; $x++) {
$rgb = imagecolorat($sourceImage, $x, $y);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
$gray = round(.299*$red + .587*$green + .114*$blue);
$grayR = $gray << 16; $grayG = $gray << 8; $grayB = $gray; $grayColor = $grayR | $grayG | $grayB;
imagesetpixel ($sourceImage, $x, $y, $grayColor);
imagecolorallocate ($sourceImage, $gray, $gray, $gray);
}
}
$destinationImage = ImageCreateTrueColor($img_width, $img_height);
imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);
imagejpeg($destinationImage, $destinationFileName);
imagedestroy($destinationImage);
imagedestroy($sourceImage);
?>
Copy-paste, replace the file names on the top and there you go (picture files must be in same folder as this script. If not, you will have to do your own file management).
annonymous at example dot com
15-Feb-2004 06:25
in addition to code_couturier - try this formula to calculate gray-value (luminance) in his "more exact" way:
$gray = round(.299*$red + .587*$green + .114*$blue);
code_couturier at graffiti dot net
18-Oct-2003 02:08
# very fast way to generate a grayscal-
# image from a true color image
#...
# --- quick grayscale image
for ($y = 0; $y <$img_height; $y++) {
for ($x = 0; $x <$img_width; $x++) {
# here we extract the green from
# the pixel at x,y , to use it as gray value
$gray = (ImageColorAt($image, $x, $y) >> 8) & 0xFF;
# a more exact way would be this:
# $rgb = ImageColorAt($image, $x, $y);
# $red = ($rgb >> 16) & 0xFF;
# $green = (trgb >> 8) & 0xFF;
# $blue = $rgb & 0xFF;
# $gray = (int)(($red+$green+$blue)/4);
# and here we set the new pixel/color
imagesetpixel ($image, $x, $y,
ImageColorAllocate ($image, $gray,$gray,$gray));
}
}
# ...
mail at laeubi dot de
09-Oct-2003 02:45
This function don't work properly for me on trucolerimages (have not tried yet for other types) it jsut produce a part-grayscale image, and some color get mesed up.
I found a workaround here:
http://www.phpbuilder.com/columns/cash20030526.php3?page=2
[quote]
Advanced Image Editing Under the GD Library
Colorizing
Colorizing images is fairly easy to do. The easiest way to colorize an image is fairly simple to grasp. Create an image of the same dimensions and fill that image with the color you want to change it to. This new image is then placed on top of the older image, giving it a colorized look.
<?php
function imagecolorize(&$im,&$col,$pct) {
$im_w = imagesx($im);
$im_h = imagesy($im);
$setpixel = imagesetpixel($im,$im_w,0,$col);
$index = imagecolorat($im,$im_w,0);
$rgb = imagecolorsforindex($im,$index);
$r = $rgb["red"];
$g = $rgb["green"];
$b = $rgb["blue"];
$layover = imagecreate($im_w,$im_h);
$color = imagecolorallocate($layover,$r,$g,$b);
$fill = imagefill($layover,0,0,$color);
$merge = imagecopymerge($im,$layover,0,0,0,0,$im_w,$im_h,$pct);
imagedestroy($layover); }
?>
If we use a blue layover RGB(0,0,255), we get this result:
[/quote]
if you use black or gray, its not perfekt, but better than nothing ;)
| |