简体   繁体   English

有关PHP中图像缩略图性能的问题

[英]Question about image thumbnail performance in PHP

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. 我在PHP中有一个函数,可以将图像调整为缩略图大小,我的图像上传脚本会获取一个已上传的图像,然后运行此函数以调整图像的大小(如果图像宽于700像素,然后还要运行该函数2次以创建2个不同大小的缩略图图片,因此每次用户上传图片时总共保存了3张图片。 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. 现在,该大小调整功能使用getimagesize()来获取尺寸,因此我的uplaod脚本调用了此功能,然后,大小调整功能再使用2-3次getimagesize()函数来制作其他大小的图像。

I am thinking that I should just pass the dimesions onto the resize function since I get them in the uploading process? 我想我应该将这些尺寸传递给resize函数,因为我是在上传过程中获得它们的?

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? 我的真正问题是,getimagesize()是资源匮乏的功能吗,是否最好至少使用它,或者在上传1张图像时可以多次调用它?

Just a tip/precausion, I asssume you're using GD functions. 只是一个提示/预防措施,我假设您正在使用GD函数。 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. 更好的方法是一次加载图像,并使用图像资源使所有带有imagecopyresampled缩略图重新imagecopyresampled -不仅将图像的尺寸传递给函数-还传递GD参考。 Thay way your original file gets loaded only once. 以这种方式,您的原始文件仅被加载一次。

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. 话虽这么说, getimagesize()并不是特别昂贵,但是如果可以一次调用它的话。 But I don't predict that much of a speed increase. 但是我并不认为速度会增加太多。 The costly part of your script is the image resizing itself. 脚本昂贵的部分是调整图像大小。

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. 否则,请等待并查看实际瓶颈在优化之前。

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. 在Windows上并在* nix上进行部署。

On my dev dox, I use xdebug + WinCacheGrind to read the results. 在我的dev dox上,我使用xdebug + WinCacheGrind读取结果。

I could not live without them. 没有他们,我活不下去。 :) :)

http://elrems.wordpress.com/2008/02/12/profiling-php-with-xdebug-and-wincachegrind/ http://elrems.wordpress.com/2008/02/12/profiling-php-with-xdebug-and-wincachegrind/

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. 您应该在以后的时间作为cron作业调整图像的大小。 You can use a third party application like imagemagick or some other resizing application to resize images. 您可以使用第三方应用程序(例如imagemagick)或其他大小调整应用程序来调整图像大小。 That way you save time on the front end. 这样,您可以节省前端时间。 You can run the resize job every 5 minutes or so. 您可以每5分钟左右运行一次调整大小的作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM