简体   繁体   English

仅当页面上没有其他任何内容时,才能在php中创建图像

[英]Creating image in php only works when nothing else happens on page

I'm using this piece of php code to create and rotate an image. 我正在使用这段PHP代码来创建和旋转图像。 And it works perfectly when i just call the img.php it lying in. 当我只是调用它所在的img.php时,它就可以完美工作。

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. 但是,如果我尝试将img.php包含在其他任何内容中,或者仅将该片段包含在另一页中(在同一服务器上),它只会显示很多问号而不是图像。

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 关于Troels

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

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. 这是预期的行为,您的脚本应该以某种类型的输出响应浏览器,通常的输出是text/html ,其中包含html内容。 To output an image, you send the Content-type: image/jpeg header, followed by image binary content (via imagejpeg($rotate); ). 要输出图像,请发送Content-type: image/jpeg标头,然后发送图像二进制内容(通过imagejpeg($rotate); )。

The errors showing in your case are probably due to you are trying to send the header after echoing html/text content. 在您的情况下显示的错误可能是由于您在回显html / text内容后尝试发送标头。 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. 要在php生成的html中输出由php生成的图像,您需要将其分为两页,一个页面用于输出图像,另一页面用于输出html,html输出将通过常规<img>标记引用您的图像,如@ GSto提到。


EDIT: 编辑:

page.php: 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: 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. 您将在浏览器中访问第一页,该页面将显示<img>标记,请求下一个文件,并获取显示为该标记的内容。

You cannot call imagejpeg() on the same page as your HTML content. 您不能在与HTML内容相同的页面上调用imagejpeg()

Browsers expect images to received in a seperate request then your HTML content. 浏览器希望图像是在单独的请求中接收的,然后是HTML内容。 If you want to embed a PHP generated image inside your page, you must have two different PHP scripts. 如果要在页面内嵌入PHP生成的图像,则必须具有两个不同的PHP脚本。

For example, you can have a file called my_page.php which generates the wanted HTML markup: 例如,您可以拥有一个名为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>

and a file called my_image.php which generates the wanted image: 还有一个名为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);

As you can see, the image is displayed by including an <img> tag which points to my_image.php . 如您所见,通过包含指向my_image.php<img>标记来显示my_image.php You cannot run imagejpeg() on the same page as your HTML content (unless using the $filename parameter, see documentation ). 您不能在与HTML内容相同的页面上运行imagejpeg() (除非使用$filename参数, 请参见文档 )。 If you have variables to pass to my_image.php , you may use GET parameters to do so. 如果您有要传递给my_image.php变量,则可以使用GET参数来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM