[英]Setting to display php as an image (.png or .jpg)
我有一个PHP源代码来绘制图像(用于绘制验证码的动态图像)。
它正在显示动态生成的png文件。 但是,当我将php移到支持PHP 5.x的免费Web服务器上而不是我公司的旧PHP 4.x服务器后,它没有显示。
我之前已经将.htaccess
文件更改为使“ html”文件包含“ php”代码。
我是否需要更改或添加.htaccess
的设置以在新服务器中将“ php”显示为图像文件?
似乎仅靠一行就行了:
header('Content-Type: image/png');
在php文件中。
我在下面添加了php文件源代码供您参考:
<?php
session_start();
// Create the image
$im = imagecreatetruecolor(150, 50);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 198, 198, 198);
//$black = imagecolorallocate($im, 0, 0, 0);
$textcol = imagecolorallocate($im, 38, 38, 38);
imagefilledrectangle($im, 0, 0, 399, 129, $gray);
// The text to draw
$text = empty($_SESSION['security_number']) ? 'error' : $_SESSION['security_number'];
$font = 'Georgia.ttf';
// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 30, 0, 10, 35, $textcol, $font, $text);
imagepng($im);
imagedestroy($im);
?>
您必须添加header('Content-Type: image/png');
在PHP中生成PNG以使其显示PNG(与JPG类似)之前。
另外,您必须在设置标头之前检查imagepng()
是否成功生成PNG。 更重要的是,您必须验证是否安装了GD Library。
同样,使用.htaccess
来使HTML文件包含PHP代码没有任何意义。 请改用PHP扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.