Benefits of imagecreate ?
mdb Offline
Junior Member
**
Posts: 27
Joined: May 2012
Post: #1
Benefits of imagecreate ?
Hey guys,
So on my forum I wanted to apply a grayscale filter to a few of my images. I did it using this method:

PHP Code:
$pic = imagecreatefrompng('images/spray.png');
imagefilter($pic, IMG_FILTER_GRAYSCALE);
imagesavealpha($pic, true);
imagepng($pic, 'images/spray_grayscale.png');
imagedestroy($pic);


However I noticed anytime someone views this page where that code is running, it re-saves the image everytime, overwriting the old one. Isn't this sort of pointless? Wouldn't it use a lot less resources if I just took away this code now since the image is already created and saved on my forum?

Forgive me but i'm new to this function and just used it for the first time. I'm not quite sure how it's intended to be used.

08-16-2012 06:59 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Benefits of imagecreate ?
Yes, I don't know why you even bothered with code in the first place.  Just filter the images yourself and upload them.

My Blog
08-16-2012 07:59 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #3
RE: Benefits of imagecreate ?
try to read the file first:

PHP Code:
1
2
3
4
5
6
if(file_exist($file))
{
read_file($file); // with the appropiate headers
exit;
}
//create the image


In my case I also check for the file date to update it (in my case it is a dynamic stats).


Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 08-16-2012 08:05 AM by Sama34.)
08-16-2012 08:04 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Benefits of imagecreate ?
And why would you do that?

My Blog
08-16-2012 02:34 PM
Find all posts by this user Quote this message in a reply
mdb Offline
Junior Member
**
Posts: 27
Joined: May 2012
Post: #5
RE: Benefits of imagecreate ?
Yeah, the problem was I was at work at the time and didn't have access to photoshop or any image manipulation software. I then researched if it was possible to add greyscale using css, finally settling on this php function. I thought it was cool until I realized it was re-saving the image each time it was viewed. It's cool but really is kind of useless...
(This post was last modified: 08-16-2012 02:44 PM by mdb.)
08-16-2012 02:44 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #6
RE: Benefits of imagecreate ?
(08-16-2012 02:34 PM)ZiNgA BuRgA Wrote:  And why would you do that?

Me? Because I already have the image created, I just paste the forum stats on it and save it to the cache directory, then, each time the file is accessed the code first check if the file exists and when was it created, if it doesn't exists or the time is too long ago, then it recreates it.

Stats are suppose to be changing frequently, it would be crazy to manually create each image every ten minutes.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 08-17-2012 06:26 AM by Sama34.)
08-17-2012 06:23 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #7
RE: Benefits of imagecreate ?
That's fair enough, but always reading a file if it exists doesn't make sense, at least from what I can gather from you.
Also if you're going to do these file based shenanigans, you'll need to ensure you don't get screwed over with race conditions and use appropriate locking.

My Blog
08-17-2012 09:17 AM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #8
RE: Benefits of imagecreate ?
(08-17-2012 09:17 AM)ZiNgA BuRgA Wrote:  That's fair enough, but always reading a file if it exists doesn't make sense, at least from what I can gather from you.

Then it would be better to redirect to the original file? Or what could probably be the best thing to do?

(08-17-2012 09:17 AM)ZiNgA BuRgA Wrote:  Also if you're going to do these file based shenanigans, you'll need to ensure you don't get screwed over with race conditions and use appropriate locking.

Sorry if I don't understand what you mean Zinga Burga. Do you mean many users accessing the file at the same time or something similar? If so, what would be a appropriate locking then?

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
(This post was last modified: 08-17-2012 10:38 AM by Sama34.)
08-17-2012 10:37 AM
Visit this user's website Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #9
RE: Benefits of imagecreate ?
I would suggest directly linking to the static image file and periodically update that.

My Blog
08-17-2012 12:21 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #10
RE: Benefits of imagecreate ?
hm.. I can as well create a task to create the image based in a interval of time, currently other problem I see is that it runs two queries each time the image is accessed, I suppose that will be a resources issue at some point. Thanks for pointing out that Zinga Burga!

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
08-17-2012 02:25 PM
Visit this user's website Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: