What is the best and fastest way to check if the image is valid in PHP?

前端 未结 6 1351
别那么骄傲
别那么骄傲 2021-01-01 11:07

What is the best and fastest way to check if the image is valid in PHP ? I need it to be able to check GIF, JPG as well as PNG images.

6条回答
  •  一向
    一向 (楼主)
    2021-01-01 11:42

    I use this function... it checks urls too

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

提交回复
热议问题