Jump to content

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


purefusion

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 :)
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.