PHP display as image - php

I am trying to make a PHP page load an image and display, as if it were an image file. Here's what I tried to do:
header("Content-type: image/png");
echo base64_encode(file_get_contents($ad->location));
But this doesn't seems to work. How should this be done?
Thank you.

You don't need to base64 encode image data when outputting it as a file.
Just pass it through.

Related

PHP cannot load image and display it

I am trying to make a php script to load image and display it depending on $_GET paramters.
I want to use this later to load image into my mobile application(Android).
The problem is I cannot make image to display. I use the code below:
header("Content-Type: image/jpeg");
readfile($path);
But when I open page in firefox I get error:
Image cannot be displayed because it contains errors.
What is more I tried different approach and I can get image display in html code(I dont want this - it requires to remove headers in java android - but it proves that path is correct)
echo '<img src="data:image/jpeg;base64,'.base64_encode(file_get_contents($path) ).'"/>
What am I doing wrong? Any ideas?
Thanks in advance.

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).'">';

How do I directly display an image using PHP?

I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
You need to set the header Content-Type property to be the correct image type. See this link for the some examples.
http://www.electrictoolbox.com/image-headers-php/
Choose the correct header :
header("Content-type: image/png");
And use php gd to change the image size : http://php.net/manual/fr/book.image.php

data url image save as png

Lets say I have this data url from Apple:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6%2BR8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH%2BIO2BOJX0XwA2%2BQEL4pAhSX4aBQAAAABJRU5ErkJggg%3D%3D
That I want to save as a png file.How do I do That?
Rightclicking and 'Save image as' is not working(Why?)
So I thought I should use a PHP script with the 'base64_decode' function:
$img = "iVBORw0KGgoAAAANSUhEUgAAA9QAAAAkCAMAAABfcIIyAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAHVQTFRFAAAA%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F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qLpPJDae9P5%2FmrVz491%2FWekBlKy7PXVfn9rb9xXsMD2wdOcAAAAAElFTkSuQmCC";
header("Content-type: image/png");
echo base64_decode($img);
That is giving me back the broken image icon :
How should I solve this problem and save a data url image to a file?
Your code is fine, but your base64 is urlencoded. I urldecoded it for you: iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAlElEQVR42oWQQRHEIAxF10ElVAISVgo3bCABCUhYCZWAhEpAQpoD6bzJhNnDO0DyyA8fEXkppXyVCpLViDUfyqVIQN9JFMY637hrlCySFauL21E7KVWbAIGx56rnSLqc5KPXSLo3kySalPhTygFhRDtFC09EIsMeZJSGBj7Qveh3OJW89syImiH+IO2BOJX0XwA2+QEL4pAhSX4aBQAAAABJRU5ErkJggg==
or do it yourself:
base64_decode(urldecode($img));

Categories