处理大量图像时,file_put_contents()非常慢 [英] file_put_contents() is very slow when working with lots of images
问题描述
我必须将〜5,000张图像保存到磁盘,并且脚本运行非常缓慢.除其他外,我的脚本包含一个foreach
循环,该循环可将图像保存到磁盘.我想问题出在file_put_contents
函数,因为当我评论它时,一切都很好.
I must save ~5,000 images to disk and my script is running very slow. Among others, my script contains a foreach
loop which saves images to disk. I guess the problem is with file_put_contents
function, because when I comment it, everything works super fine.
这是我的代码:
file_put_contents($pathimg . '.jpg', file_get_contents($url));
file_put_contents($pathimg . '.jpg', file_get_contents($url3));
有没有一种方法可以更快地保存这些图像?
Is there a way to save these images faster?
推荐答案
您发布的代码在同一行中执行file_get_contents和file_put_contents.您看到的缓慢速度可能是映像下载(file_get_contents)或磁盘写入(file_put_contents)-但是,写入速度与将映像写入磁盘的速度几乎可以忽略不计.
The code you posted is doing file_get_contents and file_put_contents in the same line. The slow speed that you're seeing can be either the image download (file_get_contents) or the disk write (file_put_contents) - However, the speed of write would mostly likely be negligible to that of writing the image to disk.
您可能应该分别对它们进行基准测试,以找出造成缓慢行为的原因.
You probably should benchmark both of them separately to find out what's causing the slow behaviour.
这篇关于处理大量图像时,file_put_contents()非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!