2

I'm using this piece of php code to create and rotate an image. And it works perfectly when i just call the img.php it lying in.

But if i try to include the img.php in anything else, or just include the snippet in another page (on same server) it just shows me a lot of questionmarks instead of an image.

My feeling is that the problem is related to the headers sent, but honestly i have no idea.

Any suggestions how to make this useful so it can be included in another page and still work?

<?php 
   header("Content-type: image/jpeg"); 
   // option one  
  // create a 250*77 image 
  $im = imagecreate(250, 77); 
  // option two  
  // creating a image from jpeg  
  $im = @imagecreatefromjpeg("images/test.jpg")  
  or die("Cannot Initialize new GD image stream"); 

  // white background and black text 
  $bg = imagecolorallocate($im, 255, 255, 255); 
  $textcolor = imagecolorallocate($im, 0, 0, 0); 

 //create the text
  $skrivetekst = date('d.m.Y');
    $skrivetekst2 = date('H:i:s');

 // write the string at the top left 
  imagestring($im, 5, 0, 0, $skrivetekst, $textcolor); 
  imagestring($im, 5, 0, 20, $skrivetekst2, $textcolor); 


 ///Rotate the image 
$rotate = imagerotate($im, 90, 0);
  // output the image 


// Output
imagejpeg($rotate);


  ImageDestroy ($rotate); 
?>

Regards Troels

4
  • What do you mean by "a lot of question marks"? Does the image fail to load? What does Firebug's net tab say about whether it gets loaded? Commented Sep 8, 2010 at 17:25
  • Here you can see the questionmarks: tm-design.dk/kontrabande Commented Sep 8, 2010 at 17:32
  • or probl better, a screendumb: tm-design.dk/kontrabande/problem.png Commented Sep 8, 2010 at 17:43
  • 1
    that's the binary of your outputted image, it will show asif you opened a jpeg image in a text editor. Commented Sep 8, 2010 at 17:59

3 Answers 3

5

probably because you are using different header types on a page, and it's not using the image header. If you want to put it on a page, i would put it in an image tag and call it that way:

<img src='img.php'>
Sign up to request clarification or add additional context in comments.

Comments

2

This is the expected behavior, your script should response to the browser with an output of certain type, usual output is text/html, that contains html content. To output an image, you send the Content-type: image/jpeg header, followed by image binary content (via imagejpeg($rotate);).

The errors showing in your case are probably due to you are trying to send the header after echoing html/text content. Once output is sent, you can't send more headers.

To output php-generated images within php-generated html, you need to split that in two pages, one to output the image, and another to output html, the html output will reference your image via the regular <img> tag, as @GSto mentioned.


EDIT:

page.php:

<html>
 <head><title>HI!</title></head>
 <body><?php
  //here goes your html building logic
  /*If you are not using and PHP, make this page page.html*/
  ?>
  See my php-generated image: <br />
  <img src="img.php" />
  </body>
</html>

img.php:

<?php 
   header("Content-type: image/jpeg"); 
   // option one  
  // create a 250*77 image 
  $im = imagecreate(250, 77); 
  //rest of your image-generating code..

You will access the first page in your browser, that will see the <img> tag, request the next file and get the content to show as that tag.

2 Comments

But does that mean that the creation of the image has to be the first thing happening on the first page loaded?
can you give me an example of splitting the plage in 2, because i don't really get how to do that.
0

You cannot call imagejpeg() on the same page as your HTML content.

Browsers expect images to received in a seperate request then your HTML content. If you want to embed a PHP generated image inside your page, you must have two different PHP scripts.

For example, you can have a file called my_page.php which generates the wanted HTML markup:

<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <p>Look at this nice dynamic image! Generated on <?php echo date(); ?>.</p>
    <img src="my_image.php" />
  </body>
</html>

and a file called my_image.php which generates the wanted image:

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

$date = date();
$x = imagefontwidth(5) * strlen($date);
$y = imagefontheight(5);

$img = imagecreate($x, $y);
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($img, 5, 0, 0, $date, $textcolor);

imagejpeg($img);

As you can see, the image is displayed by including an <img> tag which points to my_image.php. You cannot run imagejpeg() on the same page as your HTML content (unless using the $filename parameter, see documentation). If you have variables to pass to my_image.php, you may use GET parameters to do so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.