Guillaume
Guillaume

Reputation: 51

PHP imagejpeg() displays weird characters

The problem:

I found this simple script explaining how to use imagejpeg():

<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>

When I copy/paste it in an empty php file it works just fine. I displays an image.

Now when I insert this code into one of my existing php file formated as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

***code above***

</body>
</html>

It does not return any image. Just lines of weird characters.

What I think happens:

I read other posts and I think the problem comes from my main header 'Content-Type'. But I don't understand why the 'Content-Type: image/jpeg' in the embeded code does not supersede whatever is written in the main header? It should tell imagejpeg() to display an image, not text...

Does anyone know how to fix it? Thanks.

Upvotes: 2

Views: 6884

Answers (4)

Abdo Saied Anwar
Abdo Saied Anwar

Reputation: 111

Add this line before calling imagejpeg()

header('Content-Type: image/jpeg');

That tells the browser that you are displaying an image not HTML.

Upvotes: 2

xlordt
xlordt

Reputation: 521

You can also do it this way.

<?php

    $im = imagecreatetruecolor(120, 20);
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

    ob_start ();

    imagejpeg($im);
    imagedestroy($im);

    $data = ob_get_contents ();

    ob_end_clean ();

    $image = "<img src='data:image/jpeg;base64,".base64_encode ($data)."'>";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php echo $image; ?> 

</body>
</html>

works for me.

Upvotes: 4

user1864610
user1864610

Reputation:

You can't simply embed the PHP code in your HTML document and emit the image - the browser is expecting HTML, not image data. The content-type header you emit from your PHP script isn't actually sent since by the time that part of the script is reached the headers have already been sent. Your new header is just discarded, and a warning added to your server log.

To make this work include an HTML image tag in your HTML document, and use your PHP script as its src attribute:

<body>
  <img src="myPHPScript.php">
</body>

You can apply whatever additional styling you need to it, as you would with any other image.

Upvotes: 0

Joey
Joey

Reputation: 354606

This is because it returns the image data as if you were including the image file as text. For displaying an image in HTML there is the img element:

<img src="myimage.php" alt="Pretty, isn't it?">

You should also get a warning that headers cannot be modified because they are already sent, which should tip you off that what you're doing is ... strange at best.

Put the code to return the image in a separate PHP file or otherwise make sure that on invocation you either get a HTML page or a JPEG image back. Mixing those two isn't that good an idea.

Upvotes: 3

Related Questions