2010-09-08 13 views
2
没有任何其他事情发生时才起作用

我正在使用这段PHP代码来创建和旋转图像。它可以完美的工作,当我只是打电话给它img.php它躺在英寸在php中创建图像只有在页面

但是,如果我尝试在其他任何内容包括img.php,或只包括片段在另一页(在同一台服务器上),它只显示我有很多问号而不是图像。

我的感觉是问题与发送的标题有关,但老实说我不知道​​。

任何建议如何使这个有用的,所以它可以被包含在另一个页面,仍然工作?

<?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); 
?> 

问候特勒尔斯

+0

你是什么意思的“很多问号”?图像无法加载? Firebug的网页标签说明它是否被加载? – 2010-09-08 17:25:26

+0

在这里您可以看到问题标记:http://tm-design.dk/kontrabande/ – Troels 2010-09-08 17:32:43

+0

或probl better,screendumb:http://tm-design.dk/kontrabande/problem.png – Troels 2010-09-08 17:43:27

回答

2

这是预期的行为,你的脚本应该针对某些类型的输出浏览器,通常输出为text/html,其中包含html内容。要输出图像,请发送Content-type: image/jpeg标题,然后发送图像二进制内容(通过imagejpeg($rotate);)。

在您的案例中显示的错误可能是由于您正在尝试在回显HTML /文本内容后发送标题。发送输出后,您无法发送更多标题。

要在php生成的html中输出php生成的图像,需要将它分成两页,一个输出图像,另一个输出html,html输出将通过标准<img>标签引用图像,如@GSto所述。


编辑:

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.. 

您将访问第一个页面在浏览器中,将看到<img>标记,请求下一个文件并获取显示内容那个标签。

+0

但这是否意味着图像的创建必须是第一页加载的第一件事? – Troels 2010-09-08 17:36:34

+0

你能给我一个分裂2的例子,因为我真的不知道该怎么做。 – Troels 2010-09-08 17:44:55

+0

@Troles,编辑..现在检查。 – aularon 2010-09-08 17:58:47

5

可能是因为您使用的是页面上不同的报头类型,并且它不是使用像头。如果你想要把它放在网页上,我就会把它放在一个图像标记,并调用它的方式:

<img src='img.php'> 
+0

非常感谢。 – Troels 2010-09-08 18:25:57

0

您不能在与HTML内容相同的页面上调用。

浏览器希望在单独的请求中接收图像,然后是HTML内容。如果你想在你的页面中嵌入一个PHP生成的图像,你必须有两个不同的PHP脚本。

例如,你可以有一个叫my_page.php文件生成想要的HTML标记:

<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> 

和一个名为my_image.php文件生成想要的图像:

<?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); 

正如你所看到的通过包括指向my_image.php<img>标签来显示图像。您不能在与HTML内容相同的页面上运行(除非使用参数$filenamesee documentation)。如果你有变量传递到my_image.php,你可以使用GET参数来做到这一点。