Return a PHP page as an image
The PHP Manual has this example:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
The important points is that you must send a Content-Type header. Also, you must be careful not include any extra white space (like newlines) in your file before or after the <?php ... ?>
tags.
As suggested in the comments, you can avoid the danger of extra white space at the end of your script by omitting the ?>
tag:
<?php
$name = './img/ok.png';
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
You still need to carefully avoid white space at the top of the script. One particularly tricky form of white space is a UTF-8 BOM. To avoid that, make sure to save your script as "ANSI" (Notepad) or "ASCII" or "UTF-8 without signature" (Emacs) or similar.
How to return image from php file set as img src
filesize
does not work on HTTP URLs. The docs say:
This function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
However, the HTTP wrapper does not support the stat
function. Because of this, you send a wrong Content-Length
header and the HTTP response cannot be interpreted by your browser.
I see two possible solutions:
Load the image into memory and use
strlen
:$image = file_get_contents('http://example.com/img.jpg');
header('Content-type: image/jpeg;');
header("Content-Length: " . strlen($image));
echo $image;Use the
$http_response_header
variable to read the remote response'sContent-Length
header:$img = 'http://example.com/img.jpg';
$fp = fopen($img, 'rb');
header('Content-type: image/jpeg;');
foreach ($http_response_header as $h) {
if (strpos($h, 'Content-Length:') === 0) {
header($h);
break;
}
}
fpassthru($fp);
How to return image
header("Content-type: image/jpeg");
imagejpeg($img);
Return the contents of an image in php file?
Check out readfile().
The basic idea is you send the appropriate MIME type headers (using header()) then deliver the file contents using readfile()
.
For example
<?php
// myhumbleimage.php
// Do whatever myhumbleimage.php does before the image is delivered
header('Content-Type: image/jpeg');
readfile('path/or/url/of/image/file.jpg');
exit;
Is there any php function that returns an image handle so that i can use it in img src html tag?
There are 2 more obvious ways to approach this problem.
Write the rendered image to a file
This is possible using
imagejpeg
, by passing in a second parameter of the filename, for example:$im = imagecreatetruecolor(..);
// ..
imagejpeg($im, 'image.jpg');Use URL Rewriting
You can always pass through all
.jpgs
in a special folder (for example/dynamic/image.jpg
through to your script which will render it). If you want, you could even take the filename (image.jpg
) as a parameter to your script telling it what to render if the image is dynamic.
Obviously, if your image needs to be different per request, #2 is the better option. However, #1 is faster and recommended if your image is static (ie. your imagettftext
always writes the same text over the same image).
Output an Image in PHP
$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
How to return an image created with php
imagepng($image)
, with no second parameter, will simply dump the raw PNG data out to the client (e.g. the browser). No need to write it to a file, then read that file back in.
This is clearly documented in the man page: http://php.net/imagepng and applies to ALL of the "save" functions in GD.
Related Topics
Issue Reading Http Request Body from a Json Post in PHP
Replace the Variable Price Range by the Chosen Variation Price in Woocommerce 3
How to Retract a Salted Password from the Database and Auth User
Fastest Way to Check If a String Is Json in PHP
How Does Recursiveiteratoriterator Work in PHP
File_Put_Contents - Failed to Open Stream: Permission Denied
How to Use Order by For Multiple Columns in Laravel 4
How to Call Shell Script from PHP That Requires Sudo
How to Write into a File in PHP
Http Requests With File_Get_Contents, Getting the Response Code
Find a Matching or Closest Value in an Array
Laravel 5 Pdoexception Could Not Find Driver
Use Bound Parameter Multiple Times
How to Set HTML Value Attribute (With Spaces)
Guzzlehttp - How Get the Body of a Response from Guzzle 6