将电子邮件地址显示为图像

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.