Jump to content

help with php image


anubis

Recommended Posts

Well, I made a php script that will add text to an image. Anyways, I converted it to .png, wrote an .htaccess file for it and everytime I run it my computer wants to download it.Anyways, here's the .png (actually a php):

<?$im = imagecreatefrompng("custom2.png");if(!$im){die("");}$string = $_GET['text']; $orange = imagecolorallocate($im, 255, 180, 0);imagestring($im, 2, 5, 5, $string, $orange);header("Content-type:image/png");imagepng($im);imagedestroy($im);?>

and here's the .htaccess:

<Files custom.png>ForceType application/x-httpd-php</Files>

Any help is greatly appreciated.Anubis

Link to comment
Share on other sites

the .htaccess file in my book is over kill.you already have the content-type set, that should already just display the file as an image in your browser. i think the fact that you ForceType is used is making your computer want to download it because the server is telling the browser to.My suggestion is just to get rid of the .htaccess file and see what happens.

Link to comment
Share on other sites

ok, I got rid of the htaccess, but it still wants to download it. I don't know why this is happening cause regular .png files work, but this one doesn't.EDIT: the main reason i'm trying this is because I want it to work on my IPB forums.

Link to comment
Share on other sites

I was confused, content-disposition is usually used when you are creating a download box. However, you can still specify inline:header("Content-Disposition: inline; filename=custom.png");If it's still not working, try it on another server with another browser.

Link to comment
Share on other sites

I already tried putting a space there, and i'll post my updated script:

<?php$im = imagecreatefrompng("custom2.png");if(!$im){die("");}$string = $_GET['text']; $orange = imagecolorallocate($im, 255, 180, 0);imagestring($im, 2, 5, 5, $string, $orange);header("Content-type: image/png");header("Content-Disposition: inline; filename=custom.png");imagepng($im);imagedestroy($im);?>

EDIT: now i'm getting

The image “http://arkonfx.com/images/pins/custom.png” cannot be displayed, because it contains errors.
Link to comment
Share on other sites

Yeah im sorry i can't really help you, i can just offer a little bit of offhand insight on this now( i dont use PHP's image functions at all... never had to and probably never will need to).if im correct, imagepng displays the image, and imagedestroy does what its name says, maybe its possible the header for png's are different (i've heard many things about IE not liking PNG's at all, and their use and acceptance browser wide not being well), so if you could, change possibly to JPEG's. I have no idea what content-disposition does, but i thought the content type had to be capitolized (ie Content-Type), if im wrong then i done hit the shift key one too many times.Other than that, im sorry but i can't help you.

Link to comment
Share on other sites

Well, the standard way of programming is to make sure each part works. So change your code to .php extension and try this:Contents of 'custom.php'

<?php$file = "template.png";$im = @imagecreatefrompng($file);	 if(!$im){  die("Image-file $file does not exist.");}$string = $_GET['text'];$orange = imagecolorallocate($im, 255, 180, 0);imagestring($im, 2, 5, 5, $string, $orange);header("Content-type: image/png");header("Content-Disposition: inline; filename=custom.png");imagepng($im);imagedestroy($im);  ?>

Make sure all that works. Then, if your server allows mod_rewrite, use this:add to .htaccess

<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule ^custom.png(/?)$ custom.php</IfModule>

Then, in your html, you can use:

<img src="custom.png?text=blahblah" alt="you better put alt text here" />
Link to comment
Share on other sites

if im correct, imagepng displays the image, and imagedestroy does what its name says, maybe its possible the header for png's are different (i've heard many things about IE not liking PNG's at all, and their use and acceptance browser wide not being well), so if you could, change possibly to JPEG's. I have no idea what content-disposition does, but i thought the content type had to be capitolized (ie Content-Type), if im wrong then i done hit the shift key one too many times.
About that, PNG images do not require any special headers, just the content-type. The content-disposition is pretty much there for posterity. Other then letting you say what the file name is, you can also use a content-disposition of attachment instead of inline to help force a download box. The only problem IE has with PNG images is that it does not display alpha transparency correctly (apparently, IE7 fixes that). Most images without transparency look fine in IE. Other then IE, PNGs are in wide use in modern browsers, the standard is around 10 years old now and is well-documented and includes examples. Here is a page for testing PNG transparency with your browser. IE6 gets it right 2 times out of 20.http://entropymine.com/jason/testbed/pngtrans/
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...