Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Header Types: Content-Disposition

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have a little script that gets an image from a database, and prompts to download it to the local system. It was working fine until I added a little code to insert a watermark. The new code works fine too and is indeed inserting the watermake but, rather than prompting to download, the image now simply appears on the screen! I don't see anything wrong so maybe other pairs of eyes can help.

In the code below, $im is the image with the watermark inserted and the other variables are being declaired too, but this code seems to be where the problem is:

Code:
$jpegQuality = 72;

switch ($mime_type) {
case "gif":
	ImageColorTransparent($im, $imagebgcolor);
	ImageGIF($im);
break;
case "jpeg":
	ImageJPEG($im, "", $jpegQuality);
break;
case "png":
	ImageColorTransparent($im, $imagebgcolor);
	ImagePNG($im);
break;
default:
	ImageJPEG($im, "", $jpegQuality);		 
}

ImageDestroy($im);

Header ("Content-Disposition: attachment; filename=" . $ImageName . "." . $mime_type);
Header ("Content-type: image/" . $mime_type);
Header ("Pragma: no-cache");
echo $im;

If I remark out ImageJPEG() for example, it then prompts to download but the resulting image is no good. Any ideas?

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Answering myself, I found the problem which is below in case someone else ever has a similar question.

Code:
$jpegQuality = 72;

switch ($mime_type) {
case "gif":
	[COLOR=red]ob_start();[/color]
		ImageColorTransparent($im, $imagebgcolor);
		ImageGIF($im);
	[COLOR=red]$ImageData = ob_get_contents();
	ob_end_clean;[/color]
break;
case "jpeg":
	[COLOR=red]ob_start();[/color]
		ImageJPEG($im, "", $jpegQuality);
	[COLOR=red]$ImageData = ob_get_contents();
	ob_end_clean;[/color]
break;
case "png":
	[COLOR=red]ob_start();[/color]
		ImageColorTransparent($im, $imagebgcolor);
		ImagePNG($im);
	[COLOR=red]$ImageData = ob_get_contents();
	ob_end_clean;[/color]
break;
default:
	[COLOR=red]ob_start();[/color]
		ImageJPEG($im, "", $jpegQuality);
	[COLOR=red]$ImageData = ob_get_contents();
	ob_end_clean;[/color]											 
}

ImageDestroy($im);

Header ("Content-type: image/" . $mime_type);
Header ("Content-Disposition: attachment; filename=" . $ImageName . "." . $mime_type); 
Header ("Pragma: no-cache");

[COLOR=red]echo $ImageData;[/color]

This seems to work perfectly but, if there's a simpler and/or better way, I would very much like to know what it is.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
I am not a big fan of using PHP's output-buffering commands. I've found that I can nearly always avoid the use of them with a little code-rearragement:

Code:
<?php
/*  other stuff happens  */

$jpegQuality = 72;

header ("Content-type: image/" . $mime_type);
header ("Content-Disposition: attachment; filename=" . $ImageName . "." . $mime_type); 
header ("Pragma: no-cache");

switch ($mime_type)
{
	case "gif":
        ImageColorTransparent($im, $imagebgcolor);
        ImageGIF($im);
		break;
	case "jpeg":
        ImageJPEG($im, "", $jpegQuality);
		break;
	case "png":
        ImageColorTransparent($im, $imagebgcolor);
        ImagePNG($im);
		break;
	default:
        ImageJPEG($im, "", $jpegQuality);
}

ImageDestroy($im);
?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks! I had tried every rearrangement I could think of but not that one. It works like a charm without the output buffering. Using your idea, I think I can modify a couple others scripts that are using output buffering too to eliminate it.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Yes, actually I know that but am trying to do too much at once. I wasn't thinking but I'm glad to have had your help to get me straightened out.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top