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

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

Related

Replace Output buffer after readfile()

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"/>

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.

outputting image content type using two functions

I'm using an image outside of my docroot and want to display that image inside of a function.
How do I correctly do this?
Here is what I have so far: (Both functions are in the same file)
function page(){
echo'
test<br />
'.output().'
';
}
function output(){
header("Content-Type: image/gif");
readfile( '../../../dirOutsideDocRoot/test.gif' );
}
This currently will output the image but I am not able to see the "test" above it. Is it possible to get the image into the page function using header?
The final content you send to the client can have one content-type only. Either image or text. If you want both in a single page, (when image isn't written to file) you could write the image in base64 in the following way:
data:[<MIME-type>][;charset=<encoding>][;base64],<data>
In your case,
data:image/gif;base64,<base64_encoded_image>
See Data URI scheme
Something like the following will work: (Untested)
$img = fread(fopen($path, "r"), filesize($path));
$base64 = "data:image/gif;base64," . base64_encode($img);
It sounds like what you are trying to do is to display an image and text on the same page. To do that you will have to make a page that has the text and has an img tag pointing to another php page that sends back just the image.
Your Content-Type header tells the browser that the entire page is an image. Although, that won't happen either because php will fail saying that data has already been sent so it can't output a header.
Hope this helps.
EDIT: You could also like Cthulhu said, embed the image with base64 like this:
function output(){
$imgbinary = fread(fopen('../../../dirOutsideDocRoot/test.gif', "r"), filesize($filename));
echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary).'" />';
}
For more info about base_64 encoding in php, see http://php.net/manual/en/function.base64-encode.php.
You should have two php pages:
Page 1:
echo'
test<br /><img src="page2.php"/>';
Page 2:
header("Content-Type: image/gif");
readfile( '../../../dirOutsideDocRoot/test.gif' );

PHP - send GET request and get picture in return

I need to send a GET request to my page pic.php, and I want to get a real picture in return.
For now I implemented this idea like this:
<?php
if ((isset($_GET['pic']))&&(isset($_GET['key']))){
$pic = $_GET['pic'];
$pic=stripslashes($pic);
header('Location: /content/'.$pic);
}
?>
But it's not really what I want - it redirects to image directly. What I want is to keep the same URL, but get a needed file depending on what values were submitted.
What is the best way to do that?
thx.
This example code snippet should do what you ask. I've also included code to only strip slashes if magic quotes is enabled on the server. This will make your code more portable, and compatible with future versions of PHP. I also added use of getimagesize() to detect the MIME type so that you output the proper headers for the image, and do not have to assume it is of a specific type.
<?php
if(isset($_GET['pic']))
{
//Only strip slashes if magic quotes is enabled.
$pic = (get_magic_quotes_gpc()) ? stripslashes($_GET['pic']) : $_GET['pic'];
//Change this to the correct path for your file on the server.
$pic = '/your/path/to/real/image/location/'.$pic;
//This will get info about the image, including the mime type.
//The function is called getimagesize(), which is misleading
//because it does much more than that.
$size = getimagesize($pic);
//Now that you know the mime type, include it in the header.
header('Content-type: '.$size['mime']);
//Read the image and send it directly to the output.
readfile($pic);
}
?>
I can see you doing this in two ways:
1) Return the URL to the image, and print out an image tag:
print '<img src=' . $img_url . ' />';
2) Alternatively, you could just pull the data for the image, and display it. For instance, set the header appropriately, and then just print the image data.
header("content-type: image/png");
print $img_data;
This assumes that you have the image data stored in a string $img_data. This method will also prevent you from displaying other things on the page. You can only display the image.
You can load the image, send the image headers, and display the image as such:
header('Content-Type: image/jpeg');
readfile('/path/to/content/pic.jpg');
Obviously the headers would depend on the filetype, but that's easy to make dynamic.
Not sure if I understand what you're after, but guessing that you want to load the picture in an img tag?
If I'm right you just do:
<img src=http://www.domain.com/pic.php?"<?php echo image here ?>" />
Basically you just make the source of the image the webpage you get directed to where the image is.

content-type failing in firefox when using dynamic images (PHP) in <img src="source.php">

Okay excuse the cryptic subject.
I have a system which dynamically fetches image data and does some modification on the fly.
Essentially, missing out the unimportant bits, this is my code:
$File->Filename = "testimage.jpg";
$File->Open();
$FileData = $File->Read();
header('Content-Type: image/jpeg');
echo $FileData;
The only output on the page is $FileData. Okay. When I run the script as is, Firefox presents me with a blank page and Chrome and IE give me a 'missing picture' box.
However oddly enough, when I remove the Content Type declaration, I can see the raw image data just fine. I have tested this with several images, granted all of the JPEG type but it clearly loads up the different pictures just fine, as the raw data changes successfully, and matches the raw content of the image itself.
Anyone have any idea why it would be failing to just display the image at this point?
You need to give more information in order to let the browser handle it correctly (use the correct type and length):
header('Content-Length: '.strlen($FileData),true); // EDIT: inserted the strlen function as suggested by kgb
header('Content-Type: image/jpeg');
Try setting Content-Length appropriately. Remove the trailing ?> to make sure there is no whitespace at the end of the script and ensure that the starting it at the very start of your script.
i'll combine phant0m's and Thariama's answers ;):
header('Content-Length: '.strlen($FileData), true);
header('Content-Type: image/jpeg');
die($FileData);

Categories