Page 1 of 1
PHP image generation
Posted: Thu Nov 27, 2008 11:58 am
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.
Re: PHP image generation
Posted: Thu Nov 27, 2008 1:02 pm
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.
Re: PHP image generation
Posted: Thu Nov 27, 2008 1:34 pm
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....
Re: PHP image generation
Posted: Thu Nov 27, 2008 1:38 pm
by mmj
You can just send the appropriate headers then use readfile for the image.
The current solution is wayy overkill.
Re: PHP image generation
Posted: Thu Nov 27, 2008 1:45 pm
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.
Re: PHP image generation
Posted: Fri Nov 28, 2008 4:29 am
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.
Re: PHP image generation
Posted: Fri Nov 28, 2008 5:53 am
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...
Re: PHP image generation
Posted: Fri Nov 28, 2008 6:10 am
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.
Re: PHP image generation
Posted: Fri Nov 28, 2008 6:44 am
by hector_vox
Thanks for eveyones help on this.
Re: PHP image generation
Posted: Fri Nov 28, 2008 11:52 am
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.