Page 1 of 1

Help commenting this code

Posted: Thu Apr 10, 2008 7:50 am
by ma501th
Everah | Please use the correct bbCode tags when posting code in the forums. You can use [code], [php], [{lang}] or [syntax="{lang}"] where {lang} is the lowercase string name of the language you want to parse.

Hi, I have used this code below for my flash animation. The flash is linked to the php coding below. Flash captures the image when the button is clicked and passes it to the php script to display the on to a internet broswer. I just need any help commenting this coding. Thanks:

Code: Select all

<?php
$data = explode(",", $_POST['img']);
$width = $_POST['width'];
$height = $_POST['height'];
$image=imagecreatetruecolor( $width ,$height );
$background = imagecolorallocate( $image ,0 , 0 , 0 );
//Copy pixels
$i = 0;
for($x=0; $x<=$width; $x++){
for($y=0; $y<=$height; $y++){
$int = hexdec($data[$i++]);
$color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
imagesetpixel ( $image , $x , $y , $color );
}
}
//Output image and clean
header( "Content-type: image/jpeg" );
ImageJPEG( $image );
imagedestroy( $image );
?>
ma501th is online now Edit/Delete Message

Everah | Please use the correct bbCode tags when posting code in the forums. You can use [code], [php], [{lang}] or [syntax="{lang}"] where {lang} is the lowercase string name of the language you want to parse.

Re: Help commenting this code

Posted: Thu Apr 10, 2008 12:23 pm
by Jade
If you wrote this code yourself you shouldn't have any problem commenting it. If you didn't write it then this is a great opportunity for you to learn something new about php. Try going to the documentation on php.net

Re: Help commenting this code

Posted: Thu Apr 10, 2008 3:47 pm
by RobertGonzalez
Exactly what do you need help commenting?

Re: Help commenting this code

Posted: Sat Apr 12, 2008 2:44 pm
by ma501th
i did not rite the code myself...so i found it on the web and applied to my application and it worked. I just dont know what the coding means. IF you could comment on partial of the code id be glad

Re: Help commenting this code

Posted: Sat Apr 12, 2008 5:01 pm
by RobertGonzalez
See if this helps:

Code: Select all

<?php
/**
 * Capture the form field 'img' and split it on a comma
 * 
 * Things to look out for:
 *  - This never checks for a posted form 
 *  - Soooo, this could throw errors if coming directly into this page
 */
$data = explode(",", $_POST['img']);
 
/**
 * These are setting the width and height variables from the information in the form
 *
 * Things to look out for:
 *  - Again, these form fields are not ever checked so they could very well throw an error
 */
$width = $_POST['width'];
$height = $_POST['height'];
 
/**
 * This creates a new image resource handle for an image with the passed arguments 
 * as the dimensions.
 *
 * Things to look out for:
 *  - If there is not form this will puke all over the place.
 */
$image=imagecreatetruecolor( $width ,$height );
 
/**
 * This sets the images background color - apparently to black
 */
$background = imagecolorallocate( $image ,0 , 0 , 0 );
 
/**
 * This copies pixels, pixel by pixel, from the $data variable
 * 
 * There is some magic happening here, keep a close eye...
 */
$i = 0;
for ($x = 0; $x <= $width; $x++) {
  for ($y = 0; $y <= $height; $y++) {
    // Set up the hexidecimal value of of $ith value incrementing $i in the process
    $int = hexdec($data[$i++]);
 
    // Sets a color into the image resource, the colors being created by bitwise value checking of the $int var
    $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
 
    // Set the pixel into the image resource
    imagesetpixel ( $image , $x , $y , $color );
  }
}
 
/**
 * Set the appropriate output header to let the user agent know what to do with it.
 */
header( "Content-type: image/jpeg" );
 
/**
 * Send the image to the user
 */
ImageJPEG( $image );
 
/**
 * Free up the image resouce
 */
imagedestroy( $image );
?>

Re: Help commenting this code

Posted: Wed Apr 16, 2008 12:59 pm
by ma501th
thanks man u been great help. Can you advice to me a site which can teach me about specific code which has been applied here so i can learn in depth.

thanks

Re: Help commenting this code

Posted: Wed Apr 16, 2008 1:02 pm
by John Cartwright
I know of a great site, it's called PHPDN: PHP Developer's Network ;)

and of course there is http://phpgd.com/