Jump to content

imagesetpixel() Troubles


Taorluath

Recommended Posts

I'm trying to make a PHP script that increases the blue value of each pixel in an image by 1.

It's important that the script goes pixel by pixel.

For some reason, however, it outputs an image that is one solid color; a brownish red block.

Why isn't this script only slightly increasing the values?? ??? ???

 

 

 

<?php
$img = imagecreatefromgif('logo.gif');

// get height and width
$h=imagesy($img);
$w=imagesx($img);

//display image
echo "<img src='out.gif' />";

//cycle through every pixel and add 1 to the blue value of the pixel's rgb value
$y=1;
while($y < $h) {
$x=1;
while($x < $w) {

$rgb = imagecolorat($img, $x, $y);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >>  & 0xFF;
$blue = $rgb & 0xFF;
$blue++;

//define the new color and assign it to the pixel
//this is where things go wrong
$color = imagecolorallocate($img, $red, $green, $blue);
imagesetpixel($img, $x, $y, $color);
$x++;
}
$y++;
}

echo imagegif($img, 'out.gif');
?>

 

I know the problem is in these two lines by the way.

$color = imagecolorallocate($img, $red, $green, $blue);
imagesetpixel($img, $x, $y, $color);

Link to comment
Share on other sites

Allocating a new color in the pallette for every pixel doesn't look like a good idea.

 

You could try attacking the pallette rather than every single pixel. Much quicker.

 

<?php

$img = imagecreatefromgif('baa.gif');


//cycle through pallette add 1 to the blue value of color's rgb value
$t = imagecolorstotal($img);
for ($i=0; $i<$t; $i++)
{
        $cols = imagecolorsforindex($img, $i);
        $blue = $cols['blue'] < 255 ? $cols['blue']+1 : $cols['blue'] ;
        imagecolorset($img, $i, $cols['red'], $cols['green'], $blue);
}

imagegif($img);
?>

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.