4

I have a dynamic image I'm trying to create, which previously worked on one web host, until I found out they removed some features, some of which broke my image. I recently moved to hostgator, and I can't get the image to work on their site either. The actual PHP script isn't important, as I am entirely sure it works, because when I go to /image.php it displays the image, but when I go to /image.jpg it does not.

What I am trying to do here is, have PHP code inside a file with a JPG extension, which is what I have at /image.jpg, not an actual image, but PHP code. I need the server to parse the JPG as PHP code, but return a JPG so that I can use the image in places that only allow images with image extensions, such as forums. The forum I am on will not accept the image with the PHP extension, that's why I need this to work as described.

The only thing I have found and tried was putting this in .htaccess:

<Files image.jpg>
ForceType application/x-httpd-php
</Files>

The .htaccess file is in the same directory as the image, and this method also used to work, but not anymore. I've tested the image as PHP with hostgator and it works fine, I just need a way to use /image.jpg properly again.

In case it helps, what my PHP script does is, takes a background image, uses ImageTTFText to alter it with things such as, a view count, an IP address display, and a few other things. As I said, the script itself works, I tested it on hostgator already, but I need to be able to put the script inside the JPG file, and have it so that when someone views the JPG, it displays the image as it's supposed to.

Any help is greatly appreciated, thank you.

EDIT: The Rewrite trick in .htaccess didn't work either. It redirects image.jpg to image.php, and the forum still won't let me use the image. Also, as for Apache's config files, I'm pretty sure I have access to that. I'm not very experienced with this, but in the cPanel I have access to Apache Handlers, which says it lets me control how the web server handles certain file types. I just don't know what to enter here for it to work as I need it, but I think it may be what I need. If anyone knows if this would help, or how to use it, please give some advice. Thank you.

EDIT: I don't know if this is what you mean, but here are the image creation parts of the script:

Header ('Content-type: image/jpeg');
Header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
Header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
Header('Pragma: no-cache');

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

$img_width = 514;
$img_height = 128;

imagepng($image);
imagedestroy($image);
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
ArbiterNST
  • 41
  • 1
  • 4
  • It would be much easier to help you if you could provide us a link to the actual script so we can examine the output. – lbrandao Jul 03 '11 at 20:36

1 Answers1

2

You can try something like

RewriteEngine on
RewriteRule image.jpg image.php [R=301]

edited to add:

In case you didn't know, you put this in your .htaccess file. With the proper path/to/file of course.

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • 1
    Was going to suggest this myself as well - probably the most likely thing to work in the case he doesn't have access to apache's config files. – Jani Hartikainen Jul 02 '11 at 20:39
  • The Rewrite trick in .htaccess didn't work either. It redirects image.jpg to image.php, and the forum still won't let me use the image. Also, as for Apache's config files, I'm pretty sure I have access to that. I'm not very experienced with this, but in the cPanel I have access to Apache Handlers, which says it lets me control how the web server handles certain file types. I just don't know what to enter here for it to work as I need it, but I think it may be what I need. If anyone knows if this would help, or how to use it, please give some advice. Thank you. – ArbiterNST Jul 02 '11 at 23:57
  • and you made your actual script as image.php and are using image.jpg in the forum, right? Idea is that the forum should accept image.jpg because it looks for the extension, but when the request is made, server redirects to image.php (your actual script), which then outputs the image. I know this trick works on some forums...I actually do this on SMF boards to have a dynamically generated avatar and a random quotes sig. – CrayonViolent Jul 03 '11 at 15:25
  • Also, your script does output an actual image for the response, right, with correct image headers and everything right? Anyways... if you have access in cPanel, you can also try this: look at your file mime types. Look at the one for php. Change your jpg to point to the same thing as your php. This will cause the server to parse *.jpg as if it were a php, running it through php. Note though that this will happen for ALL jpg files... – CrayonViolent Jul 03 '11 at 15:29
  • I'm not sure about image headers or anything. All I do know is, what I have now used to work perfectly, now I'm just trying to get it to work on this host. – ArbiterNST Jul 03 '11 at 18:40
  • well...here is a tutorial I wrote a while back, about this very thing: http://www.phpfreaks.com/tutorial/php-add-text-to-image . Your script should more or less look like this (except with jpg functions and headers (I use png)) , so you can compare. Basically your script should be doing what my tutorial script does, and then you would use it alongside the .htaccess redirect – CrayonViolent Jul 03 '11 at 18:50
  • yes, that is what I'm talking about (well, you deleted your comment...but yes, that was what I was talking about)...except that you are using imagepng() but setting your headers to use jpeg... you should be matching all that up to use the same image format – CrayonViolent Jul 03 '11 at 18:51
  • OMG I just fixed it. I'm not an expert, but I noticed that at the same time also, that's why I removed the comment. I changed imagepng to imagejpeg and put AddHandler application/x-httpd-php5 .jpg in the .htaccess file and it all works. Thank you! – ArbiterNST Jul 03 '11 at 18:55
  • yeah the AddHandler should now indeed work as an alternative as well...however, IMO you should stick with the redirect method in my answer...with the AddHandler, you are making your server parse ALL .jpg files as php, whereas with the redirect method, you are just redirecting a specific .jpg request to a script. – CrayonViolent Jul 03 '11 at 18:57