問題描述
可以加快 php/imagick 中的圖像加載速度 (Possible to speed up image loading in php/imagick)
我注意到在 php 中將圖像加載到 imagick ($im = new Imagick($sFilename);
) 對於 8MB 圖像需要 0.6 秒。這對我來說似乎有點慢,所以我嘗試了一個測試並使用 file_get_contents
讀取文件。大約 0.005 秒。更好的。有點太好了,我猜那裡有一些緩存?
但是我可以將同一個文件加載到 imagick 中十幾次,它總是 ~0.6 秒。
我可以告訴 file_get_contents 以某種方式繞過系統緩存,讓我更好地了解從我的硬盤驅動器中檢索 8MB 文件的原始速度?
有什麼可以加快 imagick 的嗎? ? 或者這個操作0.6秒是完全正常的嗎?
參考解法
方法 1:
Is there anything that can be done to speed up imagick?
Buy a faster CPU
Or is 0.6 seconds for this operation completely normal?
Yes.
This seems a bit slow to me
but it seems a long time for that.
I guess there's some caching going on there?
You're just guessing that something should be faster.....and you'r comparing it to a completely different operation. file_get_contents just reads the bytes in the file off the disk. Creating an image from a JPG means the computer has to read the bytes off the disk, and then decode them from the compressed data to be the actual image data.
If you want to see how much work has to be done during the compression, you can easily see this by writing the image out in an uncompressed format e.g.
$imagick = new Imagick("./testImage.jpg");
$imagick‑>setImageFormat('BMP');
$imagick‑>writeImage("./output.bmp");
And yes, this is longer than is reasonable for a HTTP request to take processing. Which is just another reason for why not running Imagick in a webserver is a good idea, but to instead run it as a background task.
(by Codemonkey、Danack)