简体   繁体   中英

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).

It was displaying a png file dynamically generated. 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.

I had changed .htaccess file to make 'html' file contain 'php' code before.

Do I need to change or add the settings in .htaccess to display 'php' as an image file in the new server?

It does not seem work with just a line:

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

in the php file.

I added the php file source code below for your reference:

<?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'); before you generate PNG in PHP to make it display PNG (similar for JPG).

Also, you have to check whether imagepng() successfully generates the PNG before setting header. More importantly, you have to verify whether GD Library is installed or not.

Also, it does not make sense that using .htaccess to make HTML file contains PHP codes; Use PHP extension instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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