Replace Output buffer after readfile() - php

I have to do multiple output in a file with "image/jpeg" header.
First file i want to show is an image, then my script do other processing functions and at the end i want to output an other image in the same cycle.
See the code below:
<?php
header("content-type: image/jpeg");
readfile(realpath("mypath/default.jpg")); // this output my first image directly
// many other functions
echo $myotherimage->getImageBlob(); // I want that the echo of this variable REPLACE the current outbut buffer ( the readfile )
?>
There is any way for do this?
I have tried with the ob_* PHP functions, but it don't work.

you can not do that.
when you set header to content-type: image/jpeg means tell browser
I will send you an image data as a flow
Notice that it is an image not many images.
If you want to show images, you should use HTML <img> tag.
<img src="path/to/image-one"/>
<img src="path/to/image-two"/>

Related

imagepng: how to save the output in a variable and then display it using img tag

I want to save the output of the imagepng to a variable instead of dispaying it, and then want to use variable in a img tag to display the image.How can I do this?
I am using a smarty template for that I need to save the resultant image to variabe and then display it in a smarty template tpl file
Thanks it advance
imagepng — Output a PNG image to either the browser or a file
Try as below :
ob_start();
imagepng($png);
$imagedata = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/png;base64,'.base64_encode($imagedata).'"/>';
Try using streams. Implement a stream wrapper that saves the contents written into a variable, and pass a stream of this type into imagepng.
For more details, see image outputs.

Display png image from php on a certain position on a webpage

The code works fine if i display only image with the help of code.
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
But if i use some other fields like echo some text or i want to put some buttons on my page.
I get error, for code:
<?php
echo "hi";
?>
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
It gives me error : The displayed page contains some errors.
Can someone please help me in this regard.
Any output before the Content-Type header will break your code. The browser does not know you are trying to serve it an image since it will have already defaulted to text/html by the time your image data turns up. If you want an image at a given point in your page, you will need to serve it as a separate object. The easiest way is to wrap it in an <img> tag e.g. <img src="myimage_generator.php" />
This answer is based off of another SO answer. Your problem is that you're trying to send header info after you already sent data to the browser, which is not possible. Even so you can't display an image on a page with it's data alone. You need to base64 encode the image data first. This way you can build a whole HTML page and place this image anywhere on it and position it with CSS.
// Enable output buffering
ob_start();
imagepng($base_image);
// Capture the output
$imagedata = ob_get_contents();
// Clear the output buffer
ob_end_clean();
echo '<img src="data:image/png;base64,'.base64_encode($imagedata).'">';

Showing email addresses as an image

I'm trying to use Php's gd library to show emails on a my page as an image, so they can't be used by spammers. The problem is that the tutorial I am following uses a header with Content-type: image/jpeg. This means that I can only have images on this page.
How can I use the GD library to show the image in an Html/Php page? Or is there any other way of doing it?
When you change the header of the page, the whole page is rendered according to that header. In the case of using something like emails, you have a few options if you want to keep it single-paged.
Your first option is to use base64 encoding.
//create the image, $im
//omit the header settings
ob_start();
imagejpeg($im);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpeg;base64,'.base64_encode($data).'">';
Here we place an output buffer to stop anything from going to the client, and store it in our variable $data. Then we just encode it using base64_encode and output to the page the way that the browser can interpret it.
There's also a get method that you can use.
if(isset($_GET['email'])) {
header("Content-type: image/jpeg");
//create the image, $im
imagejpeg($im);
imagedestroy($im);
die;
}
echo '<img src="?email">';
Really, we're just sending an email input and processing with an if statement.

PHP inside img src

Trying to see what actions can be performed with a PHP script that is being called via an image src like so:
<img src="http://example.com/script.php" />
Now, I have tried to include the PHP header() function in script.php:
<?php
header("Location: http://example.com");
I have also tried to echo an image url expecting the img to display it, which it didn't:
<?php
echo 'http://example.com/image.png';
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
No. A resource that is used as a src for an img tag needs to output image data, nothing else.
There are some exceptions, eg. a header("location: ....") redirect, but the redirect needs to point to another valid image resource, not a web site as you show in your example.
Check out the readfile() as a way to output your image file from your script.php
readfile($file);
Read more about it here in the manual:
http://php.net/manual/en/function.readfile.php
where Example #1 gives an idea of how to set up the headers.
The manual also states that:
readfile() will not present any memory issues, even when sending large
files, on its own.
and
A URL can be used as a filename with this function
ps: This was the way Wordpress Multisite used to open user uploaded (e.g. images) files.
Your script.php should return the output of an image with the correct headers. For instance:
<img src="/html/img/script.php" />
// Script.php
$file = "tiger.jpeg";
$type = "image/jpeg";
header("Content-Type: $type");
header("Content-Length: " . filesize($file));
readfile($file);
You should keep in mind that the src tag should directly point to an image file. However, it is possible to use PHP to create an image, for exmaple by using the GD library:
http://php.net/manual/en/book.image.php
So using:
<img src="http://example.com/script.php" />
is possible, as long as script.php really outputs an image file, for example by using the example as described here:
http://www.php.net/manual/en/image.examples-png.php
I used this kind of processing in the past to overlay texts on JPG images for a broker website (e.g. new, sold, for rent, etc.).
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Yes, but the PHP Script has to output image data only, as stated in various other answers.
With that being said, just read the image and output it to the stream with readfile
header('Content-Type: image/png');
readfile($file);
exit();
I know I might be a couple years late to really help you, but the accepted answer just isn't true (anymore).

PHP Debugging: How to view image resource information

I am debugging a PHP application that handles image resources. I would like to see the output ($dst_image as per the PHP manual's jargon), but the code is not in a place that I could simply output it to the browser. Would the best debugging procedure be to write $dst_image to a file, and to load that file in the browser? Any other ideas?
Thanks.
See Example #1 and #2, imagejpeg will output the jpeg data.
You need to do few tings:
set headers for image header('Content-Type: image/jpeg');
output your image data
Make sure you are not outputting any data except the image
At the end you should end up with something like this:
<?php
// Get new dimensions
// Resample
// etc...
// set header so browser can render image properly
header('Content-Type: image/jpeg');
// Output
imagejpeg($image, null, xxx);
// [or]
echo file_get_contents($pathToJpgImage);
If you find your self in a situation where current request outputs data and you cannot output image... You can inject the base64 encoded image data into the HTML by using <img src="data:image/jpeg;base64,..." />. See php documentation on base64_encode for images.

Categories