Page 1 of 1

Using GD2 to output images from PHP

Posted: Wed Aug 09, 2006 10:46 am
by elbuenob
When I output as a JPEG using GD2 I do get an image but its actually a php file with a lot of code that makes up the image. Although I can view it in a browser, I can't download it as a file (it has zero size). Can I make the file so I can download it from the php output? Better yet, how can I create this file on the server as a JPEG and display it in the browser verses just outputting it? An example of what I'm doing is here: http://www.cluster-dev.com/snapshot/snapshot.html

I'm passing flash 8 bitmap data to a php file that is creating the image but I suspect there's an example doing something much simpler with user interaction. This is an example taken from:

http://flash-db.com/Tutorials/snapshot/

Thanks! Sincerely, Brandon Schmittling

Posted: Wed Aug 09, 2006 10:51 am
by feyd
It would be nice to see the code you are using.

imagejpeg() has an argument for a filename.

To get browsers, mostly IE, to recognize the image properly, you need to send a content-length header() along with the proper content-type.

PHP Code generating the Image

Posted: Wed Aug 09, 2006 10:59 am
by elbuenob
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is the code in use to generate the image:

Code: Select all

//If GD library is not installed, say sorry
	if(!function_exists("imagecreate")) die("Sorry, you need GD library to run this example");
	//Capture Post data
	$data = explode(",", $_POST['img']);
	$width = $_POST['width'];
	$height = $_POST['height'];
	//Allocate image
	$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor( $width ,$height ):imagecreate( $width ,$height );
	imagefill($image, 0, 0, 0xFFFFFF);
	//Copy pixels
	$i = 0;
	for($x=0; $x<=$width; $x++){
		for($y=0; $y<=$height; $y++){
			while(strlen($data[$i]) < 6) $data[$i] = "0" . $data[$i];
			$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 )); 
			$g = 255-hexdec("0x".substr( $data[$i] , 2 , 2 )); 
			$b = 255-hexdec("0x".substr( $data[$i++] , 4 , 2 ));
			$color =  ($r << 16) | ($g <<  | $b; 
			$color = imagecolorallocate($image, $r, $g, $b);
			imagesetpixel ( $image , $x , $y , $color );
		}
	}
	//Output image and clean
	header( "Content-type: image/jpeg" );
	ImageJPEG( $image );
	imagedestroy( $image );

------------

As I said, this does work but it makes a file in "show.php" (this file name this code is in) that can't be saved and only paste-able into certain programs (like photoshop).

http://www.cluster-dev.com/snapshot/snapshot.html


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Smiley Face explained...

Posted: Wed Aug 09, 2006 11:01 am
by elbuenob
That smiley is suppossed to be an eight, as in "open parens dollar sign g << eight close parens"

GD2 Comment...

Posted: Wed Aug 09, 2006 11:10 am
by elbuenob
Thomas Boutell of GD2 (boutell.com) says the following:

"It sure sounds to me like you're just writing some PHP code out but
not actually running any. I think you have some basic PHP confusion
here about whether your server is really running any PHP code or just
echoing it to the browser (which should never see PHP code, ever,
if your server is configured to support PHP).

See http://www.php.net for basic information like how to output the content
type you want and write an image directly to the browser with PHP
in response to an element in another page."

----------


This helps a little, but how do I make that code "run" on the server. Isn't the example doing just that already?