1

私はこの関数を書きました:

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

URLが画像タイプを返すかどうかをチェックします。返された画像の拡張子が配列(「png」、「jpeg」、「jpg」)にあるかどうかを制御するこのメソッドを拡張したいと思います。

ユーザーがアプリで画像の URL を送信するため、このコントロールが必要です。

どんな提案も感謝します!

4

5 に答える 5

1

このコード スニペットは、画像かどうかのチェックと共に画像の種類を返します。

    function get_type ($url) {

    $mimes = array (    'bmp'   =>  array('image/bmp', 'image/x-windows-bmp'),
                    'gif'   =>  'image/gif',
                    'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
                    'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
                    'jpe'   =>  array('image/jpeg', 'image/pjpeg'),
                    'png'   =>  array('image/png',  'image/x-png')
            ) ;

    $info  = getimagesize($url);

    //$info['mime'] = 'image/gif';

    if ($info['mime']) {
        if ($type = array_search ($info ['mime'],$mimes)) 
            return $type;
        else {
            foreach ($mimes as $key=>$value) {
                if (is_array ($value) ) {
                    if ( !( FALSE === array_search ($info['mime'] , $value ) ))
                        return $key;
                }
            }
        }//end of else
    }
    else return "Not an image";

}

これでこの関数を呼び出すと:

echo get_type("http://www.gravatar.com/avatar/701467343cbd095506ca24b1ee5406d8?s=128&d=identicon&r=PG");

戻りpngます。

そして、この url で呼び出す場合:

echo get_type("http://www.google.com");

戻りNot an imageます。

それが役立つことを願っています。ハッピーコーディング:)

于 2012-11-09T15:32:06.710 に答える
1

「Content-Type」ヘッダーが次のいずれかであるかどうかを確認します。

  1. コンテンツタイプ:image/png
  2. コンテンツタイプ:image/jpeg
  3. コンテンツタイプ:image/jpg

また

$url引数が「.png」、「.jpg」、または「.jpeg」で終わっているかどうかを確認します

于 2012-11-09T14:55:01.593 に答える
1

このコード スニペットは、私が正しく理解している場合に役立ちます。

$info  = getimagesize("http://www.gravatar.com/avatar/701467343cbd095506ca24b1ee5406d8?s=128&d=identicon&r=PG");
echo $info['mime'];
于 2012-11-09T14:58:49.093 に答える
0

特定の URL からファイルに関する情報を取得するには、次のコードを使用できます。

<?php
//the request
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

// get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
?>

次のように出力されます: text/html; charset=ISO-8859-1

于 2012-11-09T15:09:29.773 に答える
0

私は個人的にこのようなものを好みpreg_matchます:

    $allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
    if(preg_match("/\." . $allowed_file_types . "$/i", 'image.jpg'))
    {
         // The file extention is allowed
    }
    else
    {
        // Not allowed!
    }
于 2012-11-09T15:03:52.957 に答える