简体   繁体   English

设置将php显示为图像(.png或.jpg)

[英]Setting to display php as an image (.png or .jpg)

I have a PHP source code to draw an image (a dynamic image to draw a captcha). 我有一个PHP源代码来绘制图像(用于绘制验证码的动态图像)。

It was displaying a png file dynamically generated. 它正在显示动态生成的png文件。 But it is not displayed after I moved the php to a free web server that supports PHP 5.x, instead of my company's old PHP 4.x server. 但是,当我将php移到支持PHP 5.x的免费Web服务器上而不是我公司的旧PHP 4.x服务器后,它没有显示。

I had changed .htaccess file to make 'html' file contain 'php' code before. 我之前已经将.htaccess文件更改为使“ html”文件包含“ php”代码。

Do I need to change or add the settings in .htaccess to display 'php' as an image file in the new server? 我是否需要更改或添加.htaccess的设置以在新服务器中将“ php”显示为图像文件?

It does not seem work with just a line: 似乎仅靠一行就行了:

header('Content-Type: image/png');

in the php file. 在php文件中。

I added the php file source code below for your reference: 我在下面添加了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);


?>

You have to add header('Content-Type: image/png'); 您必须添加header('Content-Type: image/png'); before you generate PNG in PHP to make it display PNG (similar for JPG). 在PHP中生成PNG以使其显示PNG(与JPG类似)之前。

Also, you have to check whether imagepng() successfully generates the PNG before setting header. 另外,您必须在设置标头之前检查imagepng()是否成功生成PNG。 More importantly, you have to verify whether GD Library is installed or not. 更重要的是,您必须验证是否安装了GD Library。

Also, it does not make sense that using .htaccess to make HTML file contains PHP codes; 同样,使用.htaccess来使HTML文件包含PHP代码没有任何意义。 Use PHP extension instead. 请改用PHP扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM