MyBB Hacks

Full Version: Benefits of imagecreate ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.

Yes, I don't know why you even bothered with code in the first place.  Just filter the images yourself and upload them.
try to read the file first:

PHP Code:
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).

And why would you do that?
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...
(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.
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.
(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?
I would suggest directly linking to the static image file and periodically update that.
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!
Pages: 1 2
Reference URL's