Reputation: 48983
I have a function in PHP that resizes images into a thumbnail, my image upload script takes an uploaded image and runs this function to resize the image if it is wider then 700px it then also runs the function 2 more times to create 2 different sized thumbnail images, so there is a total of 3 images saved each time a user uploads an image. My resize/thumbnail function is called 2 times for the thumbnails and an occasional 3rd time if the file is to wide in dimensions.
Now this resizing function uses getimagesize( ) to get the dimensions, so my uplaod script calls this function, then the resizing function uses the getimagesize( ) function 2-3 more times to make other sized images.
I am thinking that I should just pass the dimesions onto the resize function since I get them in the uploading process?
My real question is, is getimagesize( ) a resource hungry functon, would it be best to use it at least as possible or is calling it a few times when 1 image is uploaded fine?
Upvotes: 0
Views: 926
Reputation: 374
I don't think the uploading part should be where you resize the image. You should resize the image at a later time as a cron job. You can use a third party application like imagemagick or some other resizing application to resize images. That way you save time on the front end. You can run the resize job every 5 minutes or so.
Upvotes: 0
Reputation: 136
Just a tip/precausion, I asssume you're using GD functions. When creating more than one thumbnail, the usual bottleneck is wrongly implemented image resizing functions - each time reading the original image and then saving the resized one. A better way is to load the image once, and use the image resource to make all thumbnails with imagecopyresampled
- don't only pass the dimentions of the image to the function - pass also the GD reference. Thay way your original file gets loaded only once.
Upvotes: 3
Reputation: 2450
The best thing to do is to profile your scripts.
Instead of theoretical answers which may not apply to a specific situation, you get a real answer and it is really instructive.
Also, with this habit, you will be able to :
I personally dev. on Windows and deploy on *nix.
On my dev dox, I use xdebug + WinCacheGrind to read the results.
I could not live without them. :)
http://elrems.wordpress.com/2008/02/12/profiling-php-with-xdebug-and-wincachegrind/
Upvotes: 0
Reputation: 301125
It's not particularly resource hungry - it has to open a file and read the image header.
Don't go out of your way to optimize it away - if it's easy, do it. Otherwise, wait and see where the real bottlenecks are in your application before optimizing.
Upvotes: 1
Reputation: 95464
For something that runs only on upload, it shouldn't bother so much. Uploading is not a action where the user expects super fast response. Premature optimization is the root of all evil.
That being said, getimagesize()
is not particularly costly, but if you can call it just once do so. But I don't predict that much of a speed increase. The costly part of your script is the image resizing itself.
Upvotes: 2