我正在设计我自己的PHP MVC
其中任何库文件都可以通过以下步骤使用:
-
加载文件
$this->registry->load->lib('Image');
-
从文件
$this->registry->Image->anyMethod();
访问方法
第一行加载位于lib
文件夹中的文件Image.php
,并返回一个instance
作为$this->registry->Image
然后使用该实例,可以将文件中的方法anyMethod()
访问为Controller
中的$this->registry->Image->anyMethod();
问题是,我无法输出任何图像!
以下代码如果从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();
似乎有效,但图像上没有文本。
在销毁图像后添加exit;
调用,以防止MVC系统添加任何进一步的输出:
imagepng($im);
imagedestroy($im);
exit;
此外,请检查在anyMethod()
调用之前是否没有发出其他Content-type
标头。