tags:

views:

242

answers:

5

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?

+2  A: 

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.

Andrew Moore
+1  A: 

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.

Paul Dixon
A: 

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 :

  • discover bottlenecks
  • make the difference between a micro optimization and a major optimization.

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/

Toto
+3  A: 

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.

enkrs
Thanks this is what I was thinking about doing
jasondavis
A: 

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.

Daniel
I have been considering this
jasondavis