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
https://forums.phpfreaks.com/topic/74150-dynamically-changing-color-of-photo/
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

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. =/

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

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

Archived

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

×
×
  • 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.