Help commenting this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ma501th
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2008 7:42 am

Help commenting this code

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Help commenting this code

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Help commenting this code

Post by RobertGonzalez »

Exactly what do you need help commenting?
ma501th
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2008 7:42 am

Re: Help commenting this code

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Help commenting this code

Post 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 );
?>
ma501th
Forum Newbie
Posts: 3
Joined: Thu Apr 10, 2008 7:42 am

Re: Help commenting this code

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Help commenting this code

Post 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/
Post Reply