dotibrb048760 2011-02-04 20:28
浏览 34
已采纳

查找文件是否存在,如果不存在,则还原为原始路径

I've written a function to take an image path, add some text to the end of it, then join it back together. I'm trying to write an if else branch to test if the file exists after I've appended the new text to the image.

For example, if I request the image path:

http://example.com/image1.jpg

It could be modified to this:

http://example.com/image1-150x100.jpg

The function is intended for WordPress, so takes the $post->ID argument, the custom field name, width and height of the intended target.

Here is the function:

function get_galleria_image ( $post_id, $customFieldName, $width, $height ) {

    $imagePath = get_post_meta($post_id, $customFieldName, true);

    $splitImage = explode('.', $imagePath);

    $count = count($splitImage);

    $firstHalf =  array_slice($splitImage, 0, $count-1);

    $secondHalf = array_slice($splitImage, $count-1);

    $secondHalf[0] = '-' . $width . 'x' . $height . '.' . $secondHalf[0];

    $firstHalfJoined = implode('.', $firstHalf);

    $completedString = $firstHalfJoined . $secondHalf[0];

    // if the filename with the joined string does not exist, return the original image
    if (file_exists($completedString)){
        return $completedString;
    } else {
        return $imagePath;
    }
}

I know this is crude, so any ideas to condense this code and make it "better" is welcomed. What I'm finding is that the function always returns the original image path. Can file_exists() take an absolute path like "http://example.com/image1.jpg', or will it only accept a root-style absolute path, like '/path/to/file/image1.jpg'?

  • 写回答

4条回答 默认 最新

  • dowdw44426 2011-02-04 20:36
    关注

    file_exists wont accept url path. Try this for url path to check if the given url is an image or not

    function isImage($url)
      {
         $params = array('http' => array(
                      'method' => 'HEAD'
                   ));
         $ctx = stream_context_create($params);
         $fp = @fopen($url, 'rb', false, $ctx);
         if (!$fp) 
            return false;  // Problem with url
    
        $meta = stream_get_meta_data($fp);
        if ($meta === false)
        {
            fclose($fp);
            return false;  // Problem reading data from url
        }
    
        $wrapper_data = $meta["wrapper_data"];
        if(is_array($wrapper_data)){
          foreach(array_keys($wrapper_data) as $hh){
              if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19 
              {
                fclose($fp);
                return true;
              }
          }
        }
    
        fclose($fp);
        return false;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line