Jump to content

Archived

This topic is now archived and is closed to further replies.

purefusion

GD2 - Converting and uploaded graphic to black and white and monochrome

Recommended Posts

Hey, I'm building a 'build-a-label' type system in PHP using GD2/ImageMagick type functions. What I'm looking for is a function (preferably in GD2, since I'm used to those functions) that would convert a (colored) logo to black and white, and/or a (colored or black and white) logo to a monochromatic specified color, such as red or blue. I appreciate all help on this so much!

Thanks :)

Share this post


Link to post
Share on other sites
I know Image Magick has the -monochrome function, is there anything like this in GD? What about a colorize function to convert black and white to color?

Share this post


Link to post
Share on other sites
If image is gif or png (palletted) you can change the color inthe image's pallette. This example lets you click on an image and change the clicked color to that specified in the R,G,B fields (red by default)

Save in "colorchange.php"
[code]<?php
$fn = $_GET['fn'];
$x = $_GET['x'];
$y = $_GET['y'];
$r = $_GET['R'];
$g = $_GET['G'];
$b = $_GET['B'];

$im = imagecreatefrompng($fn);
$col = imagecolorat($im, $x, $y);
imagecolorset($im, $col, $r, $g, $b);

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>[/code]

Save in test.php and run
[code]<?php

$fn = 'myimage.png'; // set to your image name

if (isset($_GET['pic_x'])) {
    $x = $_GET['pic_x'];
    $y = $_GET['pic_y'];
    $r = $_GET['R'];
    $g = $_GET['G'];
    $b = $_GET['B'];

    echo "<img src='colorchange.php?fn=$fn&x=$x&y=$y&R=$r&G=$g&B=$b'>";
}
?>

<FORM>
Convert to R<INPUT TYPE='TEXT'  name='R' value='255' size='3' maxlength='3'>
G<INPUT TYPE='TEXT'  name='G' value='0' size='3' maxlength='3'>
B<INPUT TYPE='TEXT'  name='B' value='0' size='3' maxlength='3'>
<BR><INPUT TYPE='IMAGE'  name='pic' src='<?php echo $fn ?>'>
</FORM>[/code]

hth

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.