douqiao4450 2015-07-19 06:12
浏览 45
已采纳

如何检查缓存的图像是否与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.

  • 写回答

1条回答 默认 最新

  • dotwc62080 2015-07-22 04:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?