如何检查缓存的图像是否具有相同的宽度&高度作为url图像


How can I check if the cached image has same width & height as url image?

我正在编写一个脚本,我只是从请求的图像url调整图像大小并缓存它们。我也给宽度&高度作为请求的可选参数。我缓存图像通过他们的文件名&我想涵盖一个用例,当用户请求相同的图像与不同的宽度或高度。

由于我使用的是Codeigniter的图像大小调整库,所以文件名都添加了_thumb。这就是我存储图像的方式。

在这种情况下我该怎么做?一种解决方案是检查调整大小的图像和缓存图像的md5哈希值。

我是这么想的:

            $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);
最后,

           if (md5_file($dirPath.$filename) == md5_file($dirPath.$resizedImage){
                serveimagefromcache() & deletethedownloadedfile();
           } else {
               resizetheimage() && serveit(); 
           }

我也很关心业绩,如果有其他建议,我将不胜感激。

尽管@arkascha的建议很好,但是将文件保存为filename hash更好。

,

$ext   = pathinfo($filename, PATHINFO_EXTENSION);    
$filehash = sha1($filename.$width.$height);
$filename = $filehash.".".$ext

你可以防止我遇到的问题。