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.
Related
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).'">';
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' );
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.
I'm using http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation to access private files using php. I can get the data of the file by saying $object->body. I actually want to see the image in the browser or play the video in a video player. Is there a way to do that?
I think I need something like readfile. The problem is readfile is I need the path to the file. The path is private so I cannot use that. Is there a way to do a readfile of the binary data?
I put this in the php thinking this would help but it still displays the binary data.
header('Content: image/jpeg');
header('Content-Disposition: inline; filename=IMAG0108.jpg');
echo $object->body;
You just set the content-type header and output the readfile to the browser. What I do is create a new php file, like "showimage.php", that accepts an ID or some such to know what image to display. Then I use it in a browser page: .
In showimage.php, something like:
<?php
header('Content-type: image/png');
readfile('/var/images/' . $_GET['id'] . '.png');
// or
// echo $object->body;
?>
That would read a file from the local system and output it as an image. Off the top of my head, so I might have messed up that code!
header('Content: image/jpeg');
echo $object->body;
Should work fine (for JPEGs), you need know what filetype is in question and then send appropriate content headers.
I have writen a small script to go in a Facebook App that can filter images for you. I am having trouble with the GRAYSCALE filter It seems to only display what I think is byte code for the image, instead of the image. I think this may have something to do with the headers and content type. I need to display the image filtered by PHP with this code:
header("content-type: image/jpeg");
$image = imagecreatefromjpeg("http://majik.zbrowntechnology.info/upload/zbt_1794056140.jpg");
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image);
imagedestroy($image, 'test.jpg');
on an HTML page. Any ideas?
You set Content-Type to image/jpeg but send a PNG image.
header("Content-Type: image/jpeg");
imagejpeg($image);
This should work.
BTW: imagedestroy() only has one argument
The script works fine for me, I see the grayscaled image(even with the wrong parts listed above)
If you see there the source I would guess at first, that there is any output before the headers can be sent.
Set error_reporting to E_ALL, so you can see if and where there is some unintended output.