PHP image generation
Moderator: General Moderators
-
hector_vox
- Forum Newbie
- Posts: 5
- Joined: Thu Nov 27, 2008 11:53 am
PHP image generation
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.
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
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.
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
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....
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
You can just send the appropriate headers then use readfile for the image.
The current solution is wayy overkill.
The current solution is wayy overkill.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: PHP image generation
Agreed. Just wanted to point out this is correct since some other not so good advise was given.mmj wrote:You can just send the appropriate headers then use readfile for the image.
The current solution is wayy overkill.
-
hector_vox
- Forum Newbie
- Posts: 5
- Joined: Thu Nov 27, 2008 11:53 am
Re: PHP image generation
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.
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
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
a) no, it won't cause probs.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
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
Thanks for eveyones help on this.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: PHP image generation
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.