【问题标题】:PHP Image not displaying when accessed through controller通过控制器访问时PHP图像不显示
【发布时间】:2012-03-08 07:21:56
【问题描述】:

我正在设计自己的 PHP MVC

其中任何库文件都可以通过以下步骤使用:

  1. 加载文件$this->registry->load->lib('Image');

  2. 从文件$this->registry->Image->anyMethod();访问方法

第一行加载位于lib 文件夹中的文件Image.php 并返回instance$this->registry->Image

然后使用该实例,文件中的方法anyMethod() 可以作为$this->registry->Image->anyMethod(); fromController 访问

问题是,我无法输出任何图像!

如果从Controller 访问,以下代码不起作用,但如果直接使用则有效!

代码取自http://in2.php.net/imagettftext

public function anyMethod()
{
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
}

请帮忙,我卡住了。

注意:在 imagepng($im); 之前添加 ob_clean(); 似乎有效,但图像上没有文字。

【问题讨论】:

  • 也许您的Controller 也发送了一些与您的header('Content-Type: image/png') 不兼容的标头信息?

标签: php model-view-controller image-processing controller gd


【解决方案1】:

在销毁图像后添加exit; 调用,以防止 MVC 系统添加任何进一步的输出:

imagepng($im);
imagedestroy($im);
exit;

还要检查在您的anyMethod() 调用之前是否有其他Content-type 标头发出。

【讨论】:

  • MrCode,它不工作!但是通过在imagepng($im); 之前添加ob_clean(); 似乎可以工作,但是图像上的文本没有显示,谢谢你的帮助..
  • 我建议在anyMethod()的开头加上exit;,看看输出是什么,如果有输出(甚至是空格)就会出问题。还要检查您的错误日志。
猜你喜欢
  • 2014-09-23
  • 2016-06-19
  • 1970-01-01
  • 2012-02-18
  • 2012-10-14
  • 2023-04-10
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多