Jump to content

Php image modifying

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Ok, i've been stuck with this for over 5 hours now. I don't have much experience with php, and have no idea what's wrong here. I've simplified the code as much as i can.

    $my_img = imagecreate( 120, 50 );
    $background = imagecolorallocate( $my_img, 0, 0, 255 );    
    
    for($j = 0; $j < imagesx($my_img)*imagesy($my_img); $j++){               
                $col = imagecolorallocate( $my_img,rand(0, 255), rand(0, 255), rand(0, 255));
                imagesetpixel( $my_img , ($j)%imagesx($my_img) , ($j)/imagesx($my_img) , $col);                
                               
    }  
    header( "Content-type: image/png" );
    imagepng( $my_img );
    imagecolordeallocate( $background );
    imagedestroy( $my_img );
trying to achieve something "noise-like", but somehow the process stops on certain location, and only 2 first rows get painted + some of the third row. A the weird thing is that none of the image editing functions work after that loop, like php crashes or something. There are no errormessages.

Posted Image

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You may wish to use a nested loop which is must easier to predict and is more performant. Your ($j)/imagesx($my_img) will create a floating point number and will not increment properly for example, this would be very hard to debug without printing each 6000+ iterations to verify.

You could benefit from this structure:

for(i = 0; i < imagesx($my_image); i++) { 

   for(j = 0; j < imagesy($my_image); j++) {

      $col = ... 

      imagesetpixel($myimage, i, j);

   }

}

Two notes: imagesx and y will be run 120*50 times in your loop, you may wish to remove these and use an integer constant to increase execution speed exponentially.

The function rand() will not be seeded automatically, meaning each refresh will display the same sequence of numbers (depending on how you use it however.) You may wish to use mt_rand to provide larger periods of more uniform distribution of which is seeded by default. If you do not want this (and wish for more bias random) then you could certainly use seed() on the current time, for example seed(time()) to have different results per refresh. This would be run once per execution, not once per loop.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Nested loop produces the same output, except the colored pixels were drawn vertically, as the height is defined by inner loop. But it still stops at some location.
I tried uploading it to a server, because i was suspecting my own php version might be faulty, but the online version acted the same. I also removed the randomness, and tried coloring it all the same color - stops at the same point.

when i removed the allocating process from the nested loop, it started painting correctly. I need many different colors, but it seems that allocating colors is limited somehow.

I found the problem description: PHP: imagecolorallocate - Manual
You can allocate maximum of 255 colors, which pretty much destroys my plans :/.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You will require a true colour image to allocate 24/32 bit colours: imagecreatetruecolor().

Another thing I should note (although not on the documents), you can freely use hexadecimal notation in place of imagecolorallocate (which is backwards to me)

i.e. $black = 0, or $red = 0x00ff0000.
<?php

$my_img = imagecreatetruecolor( 256, 300 );

//demonstrate 255^3 colours
for($i = 0;$i < 256;$i++) {
    for($j = 0;$j < 100;$j++) {
        imagesetpixel($my_img, $i, $j, $i);
    }
    for($j = 100;$j < 200;$j++) {
        imagesetpixel($my_img, $i, $j, $i << 8);
    }
    for($j = 200;$j < 300;$j++) {
        imagesetpixel($my_img, $i, $j, $i << 16);
    }

}



header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
    
?>
The format is of course 0xaarrggbb and it is faster to shift or use mt_rand(0, pow(2, 24)) than to use color allocation(rand, rand, rand) functions in a loop.

for($i = 0;$i < 256;$i++) {
  for($j = 0;$j < 256;$j++) {
     imagesetpixel($my_img, $i, $j, mt_rand(0, pow(2, 24)));
  }
}

Attached Files


Edited by Alexander, 02 June 2011 - 09:15 AM.
Random samples

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Thank you very much, that's exactly what i needed :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users