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
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 theContent-type: image/jpegheader, followed by image binary content (viaimagejpeg($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:
img.php:
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.