Changing the html title of a PHP created image - php

I have a php script that generates images from a URL and displays them as images with the correct header. The problem I've ran into is that I cannot change the filename/title that is displayed in the browser tab. It will display "file.php" as the title. If I try to specify the title with tags before creating the image it will cause errors and the image won't be displayed at all.
PDFs have an option to set the title, I tried to look for header titles, but couldn't find any.
This is the code, the title will show what .php file it is saved as. I would like to specify a title for the image either before or after creation.
header('Content-Type: image/png');
$im = imagecreatefrompng('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png');
imagepng($im);
imagedestroy($im);

Add a new Content-Disposition header and set the filename there
header('Content-Disposition: inline; filename="image.png"');
Specify inline parameter to not force browsers to download the file, or replace inline with attachment to force download
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives

Related

send content type image/TIFF to the browser is not working

I'm using Imagick (6.7.7-4 and module version 3.2.0RC).
I am trying to create an image and render it in the browser dynamically, for example, by HTML
<img src='image_generator.php?QUERY_PARAMS_TO_DETERMINE_TEXT'>
This is the image_generator.php content:
/* Create text */
$draw->setFillColor('white');
$draw->setFontSize( 19 );
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->annotation(0, -50, 'points');
$image->drawImage($draw);
/* Give image a format */
$image->setImageFormat('tiff'); // png works
/* Output the image with headers */
header('Content-type: image/tiff'); // png works
echo $image;
When I open image_generator.php in php and the image format is set to TIFF, it automatically downloads a PHP file and does not show an image as of the PNG case.
If changing the extension of the downloaded file to ".tif" results in the correct image, then, the problem is, the browser is using the page name as the file name. That's not uncommon when a server side script or applet is programmatically serving an image (or other file for download).
Change the "attachment" name using the following header.
/* Give image a format */
$image->setImageFormat('tiff'); // png works
/* Output the image with headers */
// This line will change the download name
header('Content-Disposition: attachment; filename="[NAME.TIFF]");
// This line will set the Mime Type
header('Content-type: image/tiff');
echo $image;
That's it.
I know I am years late to this party, but, because various people run into this and think it's bad code or configuration, or simply need to know how to rename their downloads, I figured I would answer it for others who run into it.

Why do i get broken image icon even if i set content-type="image/jpeg"?

That question might look silly but i would appreciate if i get a good answer.
I know what http header is and we can change it using header function in php.
Suppose i have a php file an_image.php and the code of it is as below :
<?php header('Content-type:image/jpeg'); ?>
<img src="image/flower.jpg">
Why am i getting a broken icon? By changing the header content type am i not changing the output as image?
As i think img tag is still an html output so as i'm trying to set an html content into an image content so i get the broken icon.
So what is the use of content-type:image/jpeg and where can it be used?
For example flower.jpg picture is in my image folder. If i create flower.php
and open the flower.jpg using a text editor and copy the code of it and paste it on flower.php and set the header content-type:image/jpeg and try to open it on browser it doesn't work saying syntax error.
Looking for a good explanation .
The correct content type for what you're outputting is text/html. You'd use the image/jpeg content type only if you were outputting the actual image file's contents itself.
<?php
header('Content-Type: image/jpeg');
readfile('image/flower.jpg');
Common uses include having a PHP script output a protected file after verifying a user's permissions allow it to be accessed, tracking pixels (save some data then serve a 1x1 image, for example), and serving dynamically generated images.

i want to change the save as dialog filename when the user right clicks

I am using PHP and javascript/jQuery for my project. I want to change the filename for the save as dialog when the user right clicks on an image and selects save as. (for example i want to name every "save dialog" filename "image.png"). Thanks in advance.
The file name that the browser gives in the save dialog CAN be changed, but not in Javascript. Usually this is done when you don't give a direct link to an image, but use a PHP script such as < img src="image.php?id=18" >
To do this you just need to send proper http headers in image.php, for example:
header('Content-type: image/jpg');
header('Content-Disposition: inline; filename="' . $filename . '"');
If you use non-ASCII file name, you will need to encode it and unfortunately the encoding is different for different browsers:
For IE use rawurlencode($filename)
For FF use base64 with charset specification, such as '=?UTF-8?B?'.base64_encode($filename).'?='
If you specify "attachment" instead of "inline" in Content-Disposition header, the browser will not try to display the image but will immediately prompt user to download it.

'Save As' image with src from PHP

I'm currently displaying images and hiding the src code by having a php file output the image. But when I right click on the image displayed and go down to 'Save As' it prompts me to download the php file not the actual image (obviously because it points to that src).
What can I do to download the actual image instead of displayImage.php?
It doesn't prompt you to download the PHP file, it simply uses that as the file name, because that is the file name from which it got the image data. If you manually input a valid image file name and try to open what you saved, it should still be a valid image.
You may also be able to give it a sensible name by including the file name in a Content-Disposition: header from your PHP file, e.g.
$filename = 'image.jpg';
header('Content-Disposition: inline; filename="'.$filename.'"');
// Don't forget the Content-Type as well...
// Output image here
...however this relies on the browser handling this sensibly, which not all of them do :-(
You can send a filename in the header.
header("Content-Type: image/png");
header('Content-Disposition: inline; filename="some.png"');
Send the correct content type in the image generator script:
header('Content-type: image/jpg');
If you want to have the .jpg extension when a PHP script is outputting an image, you'll need to do a htaccess or httpd.conf rewrite, where you can rewrite a .jpg request, to your php image generator script.
See mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html

using header() to rewrite filename in URL for dynamic pdf

I have a php script that generates a pdf report. When we go to save the pdf document, the filename that Acrobat suggests is report_pdf, since the php script is named report_pdf.php. I would like to dynamically name the pdf file, so I don't have to type the appropriate name for the report each time that I save it.
Asking on a news group, someone suggested this, where filename="July Report.pdf" is the intended name of the report
<?
header('Content-Type: application/pdf');
header('Content-disposition: filename="July Report.pdf"');
But it doesn't work. Am I doing it wrong, or will this work at all? Is this a job for mod_rewrite?
So I've tried both
header('Content-disposition: inline; filename="July Report.pdf"');
and
header('Content-disposition: attachment; filename="July Report.pdf"');
( not at the same time ) and neither work for me. Is this a problem with my web host? For this url, here's my code:
<?
header('Content-disposition: inline; filename="July Report.pdf"');
// requires the R&OS pdf class
require_once('class.ezpdf.php');
require_once('class.pdf.php');
// make a new pdf object
$pdf = new Cpdf();
// select the font
$pdf->selectFont('./fonts/Helvetica');
$pdf->addText(30,400,30,'Hello World');
$pdf->stream();
?>
Try:
header('Content-Disposition: attachment; filename="July Report.pdf"');
or
header('Content-Disposition: inline; filename="July Report.pdf"');
Another option would be to use the $_SERVER['PATH_INFO'] to pass your "July Report.pdf" - an example link might be:
<a href="report_pdf.php/July%20Report.pdf?month=07">
That file should default to saving as "July Report.pdf" - and should behave exactly like your old php script did, just change the code that produces the link to the pdf.
Should be:
header('Content-disposition: attachment;filename="July Report.pdf"');
Based on https://github.com/rospdf/pdf-php/raw/master/readme.pdf (Page 19)
The stream-method accepts an array as parameter:
stream([array options])
Used for output, this will set the required headers and output the pdf code.
The options array can be used to set a number of things about the output:
'Content-Disposition'=>'filename' sets the filename, ...
This code works well for me
$ezOutput = $pdf->ezStream(array("Content-Disposition"=>"YourFileName.pdf"));
In my case I use ezStream() but I think stream() should give the same result.
For the name shown on the title tab in the browser
I had the same problem, then i found that it's metadata missing inside my .pdf.
I used a tools ("debenu PDF tools") for edit pdf property like author, title, etc...
I just change title from empty field to what title I want, upload the new pdf and now, with same code, same script the browser show the right name!
For the name when u ask to save document
is what u specify in header filename=
Did you try to remove spaces from file name using hyphens? So, I think its name must be like this; filename=July-Report.pdf

Categories