Create an image with PHP and htaccess

Hitman
Soldato
Joined
25 Feb 2004
Posts
2,837
Hi,

Having a problem and not really sure where to start looking. I have a PHP file named 'test.png', and the htaccess file makes that run in PHP when called.

PHP code:
Code:
<?php
	header("Content-type: image/png");
	$image = imagecreate( 200, 200 );
	imagecolorallocate($image,200,100,100);
	imagepng($image);
	imagedestroy($image);
?>

As a .php file, this runs fine and a nice red square is produced. As a .png file the following error is produced:
The image “test.png” cannot be displayed, because it contains errors.

The .htaccess file:
Code:
<Files test.png>
	SetHandler application/x-httpd-php5
</Files>

Anyone know what would cause this? The site is running on CentOS 5 with cPanel v11 and PHP5.

Cheers
 
Code:
$image = @imagecreate(200, 200)or die("Cannot Initialize new GD image stream");

Add the above, it may help in debugging this problem.

Done, and the error still shows:
The image “test.png” cannot be displayed, because it contains errors.

PHP itself seems to be working fine, because if I rename the file to test.php, the image is created without any errors.

I'm clueless :confused:

Cheers

[ Edit ]

Doh, I seem to have fixed it. I changed SetHandler application/x-httpd-php5 to SetHandler application/x-httpd-php.
 
Last edited:
I'm not sure I understand what you're doing - you seem to be holding a php script in a .png file then setting the interpreter to pick up png files as well as php. seems a little unnecessary - you can do <img src="test.php" /> as long as test.php is outputting an image. to make sure is is, stick header('Content-type: image/png'); then you don't need to mess around with htaccess
 
It's a quited complex /weird script, I don't think I could explain what it does clearly if I tried :p

What "test.png" actually does is reads an (external) XML file and places the data onto an image. Users can then go to http://bleh.rawr.com/server/[their name].png (this is also done by htaccess) and it will automatically lookup the data and output it onto their own signature-type image.

Code:
<IfModule mod_rewrite.c> 
	RewriteEngine on 
	RewriteRule server/(.*).png test.png?q=$1
</IfModule>

Where $1 is their name. $1 is captured by 'test.png', and does it's thing.
 
Last edited:
Back
Top Bottom