php - 在 php 中创建图像仅在页面上没有其他任何事情发生时才有效

标签 php

我正在使用这段 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/

相关文章:

php - 如何使用 javascript/html/css 模拟 struts 验证

php - 加入其他表并在一个列中使用 group by 计算相同的 id

php - 在数据库中组织类数据的最佳方式是什么?

javascript - 使用 Ajax 将可变数据作为帖子发送

php - Wordpress 与 Xampp 本地服务器

javascript - Dojo:所有 xhr/ajax 调用似乎都是同步的并阻止其他调用

php - 如果数据库表为空,阻止显示“删除”按钮?

php - 长时间运行的 Golang 程序和资源(文件句柄、tcp 连接等)

php - 函数 split() 已弃用,preg_split() : No ending delimiter ',' found

javascript - 获取循环内单选按钮的值