Dynamic Images (GD) - Some loading, Some not....

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
GroovyinAz
Forum Newbie
Posts: 8
Joined: Tue Oct 01, 2002 6:26 pm
Location: Tucson,AZ

Dynamic Images (GD) - Some loading, Some not....

Post by GroovyinAz »

I am creating on the fly images with PHP and GD. I am using the following code to do this. When the calling page is loaded, some of the images do not display. They are replaced with the broken image placeholder. However, if I right click the image and select Show Image it is displayed. (Internet Explorer 6) Any help would be greatly appreciated.

Code: Select all

<?php 

//error_reporting(E_NOTICE);

if ($image) &#123;
	$imgfile=urldecode($image);
	$label=urldecode($label);
	if (is_file($imgfile)) &#123;
		/*
		Get Size of input file and declare/assign variables for them	
		*/
	
		$sizearray = GetImageSize($imgfile);		 
		$src_width  = $sizearray&#1111;0];
		$src_height = $sizearray&#1111;1];
		
		/* 
			Create our temporary image in porportion to the original		
			
			Create 2 times as large as $imgfile to allow for better results with TTF (true type font)
		*/
		
		$image_temp = ImageCreateTrueColor($src_width*2,$src_height*2);
		
		/* 
			Allocate our colors of course			
		*/
		
		$black = ImageColorAllocate($image_temp,0,0,0);
		$white = ImageColorAllocate($image_temp,255,255,255);
		
		/*
			Fill our background with white for transparency 
		*/
		
		ImageFill($image_temp,0,0,$white);				
		
		/*
			Render the text onto the image:
				Done 3 times to fatten up the text
				Notice the offset in each render.
			 
		*/
		
		ImageTTFText($image_temp,20,0,42,30,$black,realpath("tahoma.ttf"),$label ."\r");
			
		/*
			Create an image to resize the text/transparency layer to 
		*/
		$img_resampled = ImageCreateTrueColor($src_width,$src_height); 

		/*
			Set the transparency
		*/
			
		ImageColorTransparent($img_resampled,$white);

		/* 
			Resize the text/transparency image ($image_temp) 
		*/
		ImageCopyResampled($img_resampled,$image_temp,0,0,0,0,$src_width,$src_height,ImageSX($image_temp),ImageSY($image_temp));
		
		/*
			Create our final canvas from the background file provided
		*/
		ImageDestroy($image_temp);
		$image_final = ImageCreateFromJPEG($imgfile);

		/* 
			Merge the layers	
		*/
		
		ImageCopyMerge($image_final,$img_resampled,0,0,0,0,$src_width,$src_height,70);//,ImageSX($image_temp),ImageSY($image_temp));

		/* 
			send our header and image to the browser
		*/

		header("Content: image/png");
		
		ImagePNG($image_final);
		
		
		ImageDestroy($image_final);
		ImageDestroy($image_resampled);
	&#125; else &#123;
		header("Content: text/html");
		die ("Image File not found ($image)");
	&#125;
&#125; else &#123;
	die ("Syntax: createimage.php?\$imagestr&\$label");
&#125;
?>
GroovyinAz
Forum Newbie
Posts: 8
Joined: Tue Oct 01, 2002 6:26 pm
Location: Tucson,AZ

Post by GroovyinAz »

K, now I've at least figured out that this is only happening with PNG. If I use Jpeg output, there's no problem. What is wrong?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is this perhaps a browser bug? Do the images show if you check the script in Mozilla or any other browser that isn't IE?

Mac
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Do not use PNG, it is not supported by all browsers.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

mikeq wrote:Do not use PNG, it is not supported by all browsers.
Surely it depends on which browsers you want to support? It's good to make people aware that some things may not be compatible in all browsers, however, you shouldn't really be telling people not to use these things just for that reason. Yes, the more browsers that your site can be viewed on the better but at the end of the day which browsers you want to support is your own choice. Browser bugs in the major browsers, however, could make more of a difference what new technologies you use.

Mac
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Okay, sorry a bit too short and sweet, but just trying to save him potential hassle in the future.

You may want to look at this link

http://www.libpng.org/pub/png/pngstatus.html#browsers
GroovyinAz
Forum Newbie
Posts: 8
Joined: Tue Oct 01, 2002 6:26 pm
Location: Tucson,AZ

lol...

Post by GroovyinAz »

Thanks y'all... I think I've got her figured out. Thanks a mil for the time ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Cool, what was it?

Mac
GroovyinAz
Forum Newbie
Posts: 8
Joined: Tue Oct 01, 2002 6:26 pm
Location: Tucson,AZ

JPEG vs PNG

Post by GroovyinAz »

Im using:

Code: Select all

header("Content: image/jpeg");
ImageJPEG($image_resource,80);
Instead of:

Code: Select all

header("Content: image/png");
ImagePNG($image_resource);
And the quality is good enough for me at that level. The banding and such is too bad at much lower quality.

I still have been unable to figure out why ImagePNG didn't work. When I specified to write to a file with ImagePNG, any of the (PNG) images that were not displaying in the browser correctly were found unterminated when opened with Adobe ImageReady 7.0. Whereas the ImageJPEG files consistantly displayed properly.

The only variation between the tests, was the code above. Go figure...

Maybe a GD bug?
Post Reply