我正在使用这段 php 代码来创建和旋转图像。当我调用它所在的 img.php 时,它工作得很好。
但是,如果我尝试将 img.php 包含在其他任何内容中,或者只是将代码段包含在另一个页面(在同一台服务器上)中,它只会向我显示很多问号而不是图像。
我的感觉是问题与发送的 header 有关,但老实说我不知道。
有什么建议可以让它变得有用,以便它可以包含在另一个页面中并且仍然有效吗?
<?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);
?>
问候特罗尔斯
最佳答案
可能是因为您在页面上使用了不同的页眉类型,而没有使用图片页眉。如果你想把它放在一个页面上,我会把它放在一个图像标签中并这样调用它:
<img src='img.php'>
关于php - 在 php 中创建图像仅在页面上没有其他任何事情发生时才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3670301/