只有当页面上没有其他内容发生时,才能在php中创建图像

Tro*_*els 2 php

我正在使用这段PHP代码来创建和旋转图像.当我只是调用它所在的img.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); 
?>
Run Code Online (Sandbox Code Playgroud)

关心Troels

GSt*_*Sto 5

可能是因为您在页面上使用不同的标题类型,并且它没有使用图像标题.如果你想把它放在一个页面上,我会把它放在一个图像标签中并以这种方式调用它:

<img src='img.php'>
Run Code Online (Sandbox Code Playgroud)