如何检查缓存的图像是否与url图像具有相同的宽度和高度?
I am writing a script where I am just resizing images from the requested image url and caching them. I am also giving width & height as optional params for the request. I am caching the images by their filenames & I want to cover one use case when user requests the same image with different width or height.
Since I am using Codeigniter's image resizing library, the filenames are appended with _thumb. That's how I am storing the images.
What should I do in that case? One solution would be to check the md5
hash of the resized image and cached image.
Here's what I am thinking to do:
$ch = curl_init($url); //initialize cURL
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$dataToWrite = curl_exec($ch); //Execute the given cURL session.
curl_close ($ch); //close the given cURL session
$fp = fopen($dirPath.$filename,'w');
fwrite($fp, $dataToWrite);
fclose($fp);
Finally,
if (md5_file($dirPath.$filename) == md5_file($dirPath.$resizedImage){
serveimagefromcache() & deletethedownloadedfile();
} else {
resizetheimage() && serveit();
}
I am concerned about the performance as well, I would appreciate any other suggestions.
我正在编写一个脚本,我只是从请求的图像URL调整图像并缓存它们。 我也给宽度& height作为请求的可选参数。 我通过他们的文件名&缓存图像 当用户请求具有不同宽度或高度的相同图像时,我想要涵盖一个用例。 p>
由于我使用的是Codeigniter的图像大小调整库,因此文件名附加了_thumb。 这就是我存储图像的方式。 p>
在这种情况下我该怎么办? 一种解决方案是检查已调整大小的图像和缓存图像的 这是我想要做的事情: p>
\ n 最后, p>
我也很关注表现,我将不胜感激任何其他建议。 p>
div> md5 code>哈希。 p>
$ ch = curl_init($ url); //初始化cURL
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_BINARYTRANSFER,1);
$ dataToWrite = curl_exec($ ch); //执行给定的cURL会话。
curl_close($ ch); //关闭给定的cURL会话
$ fp = fopen($ dirPath。$ filename,'w');
fwrite($ fp,$ dataToWrite);
fclose($ fp);
code> pre>
if(md5_file($ dirPath。$ filename) )== md5_file($ dirPath。$ resizedImage){
serveimagefromcache()& deletethedownloadedfile();
} else {
resizetheimage()&& serveit();
}
code> pre>
Even though @arkascha's suggestion was quite good but saving the file as filename hash is much more better.
With this,
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$filehash = sha1($filename.$width.$height);
$filename = $filehash.".".$ext
you can prevent the issue that I was having.