By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

imagesetpixel() Troubles

P: 9
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?? ??? ???



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $img = imagecreatefromgif('logo.gif');
  3.  
  4. // get height and width
  5. $h=imagesy($img);
  6. $w=imagesx($img);
  7.  
  8. //display image
  9. echo "<img src='out.gif' />";
  10.  
  11. //cycle through every pixel and add 1 to the blue value of the pixel's rgb value
  12. $y=1;
  13. while($y < $h) {
  14. $x=1;
  15. while($x < $w) {
  16.  
  17. $rgb = imagecolorat($img, $x, $y);
  18. $red = ($rgb >> 16) & 0xFF;
  19. $green = ($rgb >> 8) & 0xFF;
  20. $blue = $rgb & 0xFF;
  21. $blue++;
  22.  
  23. //define the new color and assign it to the pixel
  24. //this is where things go wrong
  25. $color = imagecolorallocate($img, $red, $green, $blue);
  26. imagesetpixel($img, $x, $y, $color);
  27. $x++;
  28. }
  29. $y++;
  30. }
  31.  
  32. echo imagegif($img, 'out.gif');
  33. ?>
I know the problem is in these two lines by the way.
Expand|Select|Wrap|Line Numbers
  1. $color = imagecolorallocate($img, $red, $green, $blue);
  2. imagesetpixel($img, $x, $y, $color);
Feb 24 '08 #1
Share this Question
Share on Google+
1 Reply


MarkoKlacar
Expert 100+
P: 296
Hi,

Not quite sure what you're doint on line 9., you're displaying the image before anything has been done to it.

Is there a way for you to post the original picture and the result picture?

Thanks
Feb 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.