0

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?

user2471133
  • 1,377
  • 3
  • 14
  • 15
  • show your code, could/will help. Also, far as I know, showing the image in header is probably the only way. It's its way of showing the image. If someone else has an alternative way of doing it, I'd sure like to know. Just make sure you don't have any HTML output before your headers. – Funk Forty Niner Jun 16 '13 at 16:23
  • To add: In order to show the image in your HTML/PHP page, will depend on the output's variable's name. I.e. `$image` (variable) - you could then just `echo $image;` – Funk Forty Niner Jun 16 '13 at 16:26
  • Welcome to Stack Overflow User2471133. You should give the [about page](http://stackoverflow.com/about) a read if you haven't already. Also, there's one other way that involves the base64-encoded string of the image @Fred –  Jun 16 '13 at 16:29
  • @MisterMelancholy Thanks, I saw that, cheers. Glad to see there are more ways than one of doing it. – Funk Forty Niner Jun 16 '13 at 16:30

1 Answers1

1

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.

jeremy
  • 9,965
  • 4
  • 39
  • 59
  • I think you'd need an `exit` at the end of the `if` block. –  Jun 16 '13 at 16:37
  • @MisterMelancholy Yes, fixed. – jeremy Jun 16 '13 at 16:39
  • I saw it fix for a second, but then the edit was undone only second later. It's not there. –  Jun 16 '13 at 16:40
  • @MisterMelancholy That's odd. – jeremy Jun 16 '13 at 16:41
  • Sorry, I don't understand. Will this allow me to display the image inside the html page. I I'm setting the content-type: image/jpeg, then how will the browser render it as text/html? – user2471133 Jun 16 '13 at 16:43
  • @user2471133 For the first solution you're not even changing the header. For the second, you're only changing the header if "email" is set (get). – jeremy Jun 16 '13 at 16:44