PHP image generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hector_vox
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 11:53 am

PHP image generation

Post by hector_vox »

I'd like to be able to point to an image file that has it's contents regularly changed, so that <img src="me.php"> will have one picture one day, another the next with me.php serving differnt images. I could use "me.jpg" and overwrite it, but I'm try to avoid this.

My current solution for me.php is :

header("Content-Type: image/jpeg");
$image = imagecreatefromjpeg($dynamic_image_change);
imagejpeg($image, NULL, 100);

imagedestroy($image);

It's pretty ugly, makes my images lower quality, resource hungry etc.

Is there a better solution.

Thanks for any help.
draco
Forum Newbie
Posts: 7
Joined: Wed Nov 26, 2008 11:45 pm

Re: PHP image generation

Post by draco »

If your images are all in one location, then instead of having your PHP file pretend to be a JPEG you could just randomly choose an image (using PHP) and put that image inside the <img> tag.
If me.php has to display the image itself, I believe that's the only way. Unless you simply set the Content-Type like you've already done and simply echo the data inside the image file. I'm not sure that would work however.
hector_vox
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 11:53 am

Re: PHP image generation

Post by hector_vox »

Ok, good to know I'm not missing anything. I can't get to the <img =""> tag, so that's out of the window.

So I'm thinking about echoing the contents of the file, just to see if it works, I expect it will then opening some whole new world of grief. I worry that it may echo some unusual characters though.

Any other ideas....
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: PHP image generation

Post by mmj »

You can just send the appropriate headers then use readfile for the image.

The current solution is wayy overkill.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP image generation

Post by John Cartwright »

mmj wrote:You can just send the appropriate headers then use readfile for the image.

The current solution is wayy overkill.
Agreed. Just wanted to point out this is correct since some other not so good advise was given.
hector_vox
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 11:53 am

Re: PHP image generation

Post by hector_vox »

It looks like readfile is the definitive solution to this problem. I just wondered if

a) using <img src="img.php" /> is going to cause any problems, ie. will browsers go "thats not an image!"
b) If the only header I need is Content-Type: image/jpeg

Obviously, assuming it's a JPG.
hector_vox
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 11:53 am

Re: PHP image generation

Post by hector_vox »

I really have to start work on this, but it would be great if someone could just let me know if I'm doing something wildly non-standard here...
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: PHP image generation

Post by mmj »

hector_vox wrote:a) using <img src="img.php" /> is going to cause any problems, ie. will browsers go "thats not an image!"
b) If the only header I need is Content-Type: image/jpeg
a) no, it won't cause probs.
b) you should send Content-Length and maybe Last-Modified.
hector_vox
Forum Newbie
Posts: 5
Joined: Thu Nov 27, 2008 11:53 am

Re: PHP image generation

Post by hector_vox »

Thanks for eveyones help on this.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP image generation

Post by John Cartwright »

If you are posting that code in other software, such as a forum, then it may detect you are running as a php script (by looking at the image extension). If that's the case, you can use mod_rewrite to the path's extension to an images and silenty have it run the script.
Post Reply