2

A simple PHP script that I picked up from stackoverflow generates a PNG with a transparent background, writes some text on it then directly outputs it to the client browser:

$font = 25;
$string = 'My Text';
$im = @imagecreatetruecolor(300, 300);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, 30, $red, "fonts/tt0588m_.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

The scope is to obtain a simple service that feeds an image based on the parameters passed to it via URL, such as Google Charts (e.g. this QR code image).

So far, so good, except that if I click on the image generated with the code above and want to save it, the browser doesn't recognize it as being a PNG image, but a PHP script (Save as type selector has this option only), as oposed to the Google Charts example, where the resource is clearly identified as a PNG file.

How do I achieve this correct resource identification by the browser?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Andrei Oniga
  • 8,219
  • 15
  • 52
  • 89
  • 4
    Have you tried a `Content-Disposition` header to change the suggested filename? – Halcyon Nov 01 '12 at 17:03
  • Is the font you are trying to access with `imagettftext` available on the server where you are running this? – dpk2442 Nov 01 '12 at 17:06
  • @Frits van Campen I haven't tried anything, I didn't know what to :) – Andrei Oniga Nov 01 '12 at 17:09
  • Yes, the font exists and the output is there. It's just that if I click "Save as...", the image is not recognized as PNG by the browser (apparently). – Andrei Oniga Nov 01 '12 at 17:11
  • Are you sure there isn't any output before this image? Check your network log in the browser for response headers – dev-null-dweller Nov 01 '12 at 17:29
  • There isn't any other output before that, no. – Andrei Oniga Nov 01 '12 at 17:37
  • And the browser is receiving `Content-type: image/png` ? – dev-null-dweller Nov 01 '12 at 17:40
  • Yes, that's what the header value is: `Content-Type:image/png`. – Andrei Oniga Nov 01 '12 at 17:54
  • This is the live example, by the way: [http://picselbocs.com/projects/cakemyface/text.php](http://picselbocs.com/projects/cakemyface/text.php). The scope is to create a script that feeds images generated based on the URL parameters passed to it, so that that image resource can be accessed within the `src` attribute of an `` tag. – Andrei Oniga Nov 01 '12 at 18:04
  • Sorry about the dead-end link above, here's a correct live example: [http://picselbocs.com/projects/cakemyface/text.php?text=This%20is%20a%20dummy%20text](http://picselbocs.com/projects/cakemyface/text.php?text=This%20is%20a%20dummy%20text). The idea is to provide the text to generate via URL. – Andrei Oniga Nov 01 '12 at 18:12
  • Possible duplicate of [using header() to rewrite filename in URL for dynamic pdf](https://stackoverflow.com/questions/2015985/using-header-to-rewrite-filename-in-url-for-dynamic-pdf) – Waqleh Mar 12 '18 at 15:32

1 Answers1

11

Browser will use the filename from the URL as the default value in the "Save as..." dialog. You can type another name of course or save the file using the suggested name (text.php) and rename it afterwards.

You can use the Content-disposition header to "suggest" a filename to the browser. Here is an example:

header("Content-type: image/png");
header("Content-disposition: inline; filename=mytext.png");
  • inline suggests that browser should attempt to display the image. Change it to attachment to suggest that the browser should display the "Save as..." or similar dialog.
  • filename= should contain the filename of your choice.
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Well, to be honest, I did check the file-name assignment when doing drag-and-drop of the image outside of the browser (not save-as). I presumed the behaviuor would have been the same with save as but as you checked it is not. So ignore my comment... – Paolo Feb 05 '14 at 10:08