0

画像パスを取得し、その末尾にテキストを追加してから、結合する関数を作成しました。画像に新しいテキストを追加した後、ファイルが存在するかどうかをテストするためにifelseブランチを作成しようとしています。

たとえば、画像パスをリクエストした場合:

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 に答える