Jump to content


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

  • Please log in to reply
2 replies to this topic

#1 purefusion

  • Members
  • PipPip
  • Member
  • 19 posts

Posted 07 March 2006 - 09:24 PM

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 :)

#2 purefusion

  • Members
  • PipPip
  • Member
  • 19 posts

Posted 08 March 2006 - 05:56 PM

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?

#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 March 2006 - 08:49 PM

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"
$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");

Save in test.php and run

$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'>";

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 ?>'>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users