Jump to content

Dynamically changing color of photo?


kernelgpf

Recommended Posts

My problem is I'm getting an error saying my image is not a valid .png file the second time I tried to run the script.. I viewed the image, it still looks the same. I'm trying to replace the red in this image-

 

http://www.dragon-dynasty.com/waterdragon.png

 

with a hex code specified in this script. Help? Pre-thanks!

 

<?php
$v=$_SERVER['DOCUMENT_ROOT'];
function change_color($image, $old_color, $new_color, $threshold = 15)
{
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    // iterate through x axis
    for ($x = 0; $x < $image_width; $x++) {

        // iterate through y axis
        for ($y = 0; $y < $image_height; $y++) {

            // look at current pixel
            $pixel_color = imagecolorat($image, $x, $y);

            if (($pixel_color <= $old_color + $threshold) && ($pixel_color >= $old_color - $threshold)) {
                // replace with new color
                imagesetpixel($image, $x, $y, $new_color);
            }
        }
    }
}

// EXAMPLE:

// convert all red in the image to green
$image = imagecreatefrompng("$v/waterdragon.png");
$red_rbg = 16646146;
$green_rbg = 65341;
change_color($image, $red_rbg, $green_rbg, 15);
imagejpeg($image,"$v/waterdragon.png");
?> 

Link to comment
Share on other sites

Change

 

$image = imagecreatefrompng("$v/waterdragon.png");

 

to your exact path (take out the $v) to the image.. and see if you stil get the error

 

so put the image in the same directory as your script and just have this:

 

$image = imagecreatefrompng("waterdragon.png");

 

 

 

See if thats the prob

Link to comment
Share on other sites

Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'waterdragon.png' is not a valid PNG file in /home/.leucorhinos/kernelgpf/dragon-dynasty.com/test.php on line 28

 

Warning: imagesx(): supplied argument is not a valid Image resource in /home/.leucorhinos/kernelgpf/dragon-dynasty.com/test.php on line 5

 

Warning: imagesy(): supplied argument is not a valid Image resource in /home/.leucorhinos/kernelgpf/dragon-dynasty.com/test.php on line 6

 

Warning: imagepng(): supplied argument is not a valid Image resource in /home/.leucorhinos/kernelgpf/dragon-dynasty.com/test.php on line 32

 

Dun think so. =/

Link to comment
Share on other sites

<?php
$v=$_SERVER['DOCUMENT_ROOT'];
function change_color($image, $old_color, $new_color, $threshold = 15)
{
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    // iterate through x axis
    for ($x = 0; $x < $image_width; $x++) {

        // iterate through y axis
        for ($y = 0; $y < $image_height; $y++) {

            // look at current pixel
            $pixel_color = imagecolorat($image, $x, $y);

            if (($pixel_color <= $old_color + $threshold) && ($pixel_color >= $old_color - $threshold)) {
                // replace with new color
                imagesetpixel($image, $x, $y, $new_color);
            }
        }
    }
}

// EXAMPLE:

// convert all red in the image to green
$image = imagecreatefrompng("$v/waterdragon.png");

$red_rbg = 16646146;
$green_rbg = 65341;
change_color($image, $red_rbg, $green_rbg, 15);
imagepng($image,"$v/waterdragon.png");
?> 

Link to comment
Share on other sites

try

 

<?php
$v=$_SERVER['DOCUMENT_ROOT'];
function change_color($image, $old_color, $new_color, $threshold = 15)
{
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    // iterate through x axis
    for ($x = 0; $x < $image_width; $x++) {

        // iterate through y axis
        for ($y = 0; $y < $image_height; $y++) {

            // look at current pixel
            $pixel_color = imagecolorat($image, $x, $y);

            if (($pixel_color <= $old_color + $threshold) && ($pixel_color >= $old_color - $threshold)) {
                // replace with new color
                imagesetpixel($image, $x, $y, $new_color);
            }
        }
    }

return $image;
}

// EXAMPLE:

// convert all red in the image to green
$image = imagecreatefrompng("$v/waterdragon.png");

$red_rbg = 16646146;
$green_rbg = 65341;
$image = change_color($image, $red_rbg, $green_rbg, 15);
imagepng($image,"$v/waterdragon.png");
?> 

 

 

 

 

lol.. im trying all i can think of.. just work with me ;)

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.