Hi,

I'm trying to run a php script in a .jpg which will create a image file but i cant get this to work as i think it should. I basically get the PHP appearing in the browser as text.

I read a few sits that claim that placing the .htaccess file in the directory and entering this line..

ForceType application/x-httpd-php

..should convince apache to run the script and return the result as jpg. The two files sit in their own directory and i think im right that the .htaccess file in the current directory is the one referenced first.

I'm using goDaddy hosing for this one and i can see GD is enabled which i believe is necessary as well.

The script is as follows but i dont think its wrong, it sits in image.jpg.

<?php

header("Content-type: image/jpeg");

$image = imagecreatefromjpeg ("images/background.jpg");

imagejpeg($image);
imagedestroy($image);

?>

Can anyone see what i'm doing wrong? i though i had done enough research into doing this and im totally stumped as to why it does not work.

Thanks in advance,

Cheers

    Can't you just do this:

    <img src="my_image_script.php" width="100" height="100" alt="Must remember accessibility" title="My image" />

    The .php extension should cause Apache to wake up and execute the script, and the image content header you send should tell the browser to display the result as an image.

    But it's been a long while since I used PHP for images, so I may be wrong.

      No, there's absolutely no reason why you can't point an image source to a .php script - that's what I have always done. Why waste the effort of fooling Apache into thinking a .jpg is a PHP script

        Especially if sometimes it really is a JPEG, and not a PHP script that generates a JPEG - one day one of those JPEGS will contain the byte sequence 0x3C 0x3F and PHP will puke when it tries to process it as a PHP script.

          Many forums don't allow .php files as images.

          I was actually just discussing this. If it is only for a few images, could you not get apache to serve the php file when the jpg file is requested. The browser wont know the difference, as it will be fed the correct data, and there would be no mix-ups with which files to parse and which ones not to. In fact, you could probably do it with hundreds of images with the right regular expression.

            You could too use in httpd.conf file adding the .jpg extenssion on:

            AddType application/x-httpd-php .php .jpg

            All jpg files it would post process as it were PHP files, but it have the inconvenience that all jpg files are going to postprocess, a solution is to host into a dedicated server of images, and put this, 😉 Nice trick!

              madwormer2 wrote:

              Many forums don't allow .php files as images.

              Thanks for the replies everybody.

              Yes thats the problem, the image is destined to become a signature image in forums and they dont accept php as an image. I can call the PHP script and get it to display the image but i really need it to be an actuall image file.

              I dont have access to the conf file but im using .htaccess. ive tried

              AddType application/x-httpd-php .php .jpg

              and also

              ForceType application/x-httpd-php

              which i thought would make the server process the image file as php but to no avail.

              thanks

                brilliant madwormer2

                works a treat.

                thanks a lot, cheers.

                  Write a Reply...