0

我编写了一个函数来获取图像路径,在其末尾添加一些文本,然后将其重新组合在一起。在将新文本附加到图像后,我正在尝试编写一个 if else 分支来测试文件是否存在。

例如,如果我请求图像路径:

http://example.com/image1.jpg

可以修改成这样:

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

该函数适用于 WordPress,因此采用 $post->ID 参数、自定义字段名称、预期目标的宽度和高度。

这是功能:

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;
    }
}

我知道这很粗糙,因此欢迎任何压缩此代码并使其“更好”的想法。我发现该函数始终返回原始图像路径。file_exists() 可以采用像“http://example.com/image1.jpg”这样的绝对路径,还是只接受根样式的绝对路径,如“/path/to/file/image1.jpg”?

4

4 回答 4

3

file_exists 不接受 url 路径。试试这个 url 路径来检查给定的 url 是否是图像

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;
  }
于 2011-02-04T20:36:51.997 回答
2

据我所知,file_exists()仅检查本地文件系统上的文件或目录是否存在。您可能希望通过连接到另一台服务器fsockopen(),然后检查 HTTP 代码以了解该文件是否存在。

// you will need to set $host and $file

$sock = fsockopen ( $host );
fwrite ( $sock, "HEAD ".$image." HTTP/1.1\r\n" );
fwrite ( $sock, "Host: ".$file."\r\n" );
fwrite ( $sock, "Connection: close\r\n\r\n" );

// Get the first twelve characters of the response - enough to check the response code
if ( fgets ( $sock, 12 ) == "HTTP/1.1 404" )
    //file doesn't exist
else
    //file exists

fclose ( $sock );
于 2011-02-04T20:35:45.320 回答
0

为了使程序的第一部分更紧凑,您可以执行以下操作:

$completedString = preg_replace("/(.+)\.([^.]+)$/siU", "$1" . "-" . $width ."x" . $height . "$2", $splitImage);

然后使用@ayush发布的功能检查远程服务器上的文件

于 2011-02-04T20:43:53.440 回答
0
if(@getimagesize($imghttppath))
{
     echo '<img src="'. $imghttppath . '" />';
}
else
{
     echo '<img src="http://www.domain.com/images/default.jpg" />';
}

这有点像getimagesize的非正统用法,除非你关闭错误,否则它必须是@,因为如果文件不存在它会返回错误。但它就像你能得到的一样简单。使用外部 URL、本地 URL、本地绝对路径和工作目录的相对路径来检查图像是否存在并且是实际图像。

于 2011-02-04T20:44:29.303 回答