最新消息: 新版网站上线了!!!

php获取远程图片的大小宽度高度



/** 
 * Image class to simulate the getimagesize() function using the cURL and GD libraries
 * 
 * @package img_info 
 * @version 1.0.0 
 * @author MT Jordan  
 * @copyright 2014 
 * @license zlib/libpng 
 * @link http://objsql.sourceforge.net  
 */ 

class img_info 
{ 
    /** 
     * Determines how many characters will be read - set higher for larger files if getting errors
     */ 
    private $range = 500000; 
     
    /** 
     * Constructor 
     * 
     * @access public 
     */ 
    public function __construct() {} 
     
    /** 
     * Method called to simulate the GD getimagesize() function w/o fopen wrapper 
     * 
     * @access public 
     * @param  str $src 
     * @return mixed 
     */ 
    public function getimagesize( $src ) 
    { 
        if ( function_exists( 'curl_version' ) ) 
        { 
            $img_dim = $this->get_img_dim( $src ); 
            $img_info = $this->get_img_info( $src ); 
             
            return array( $img_dim[0],  
                          $img_dim[1],  
                          $img_info[0],  
                          "width=\"$img_dim[0]\" height=\"$img_dim[1]\"",  
                          'colors' => $img_dim[2],  
                          'mime' => $img_info[1],  
                          'size' => $img_info[2] ); 
        } 
        else 
        { 
            trigger_error( 'The curl extension is not enabled.', E_USER_WARNING ); 

            return false; 
        } 
    } 
     
    /** 
     * Private method returns dimensional info 
     * 
     * @access private
     * @param  str $src 
     * @return array 
     */ 
    private function get_img_dim( $src ) 
    { 
        $headers = array( 'Range: bytes=0-' . $this->range ); 
         
        $curl = curl_init( $src ); 
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );   
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers ); 
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); 
        $data = curl_exec( $curl ); 
        curl_close( $curl ); 
         
        $img = imagecreatefromstring( $data ); 
        $colors = imagecolorstotal( $img ); 
        $width = imagesx( $img ); 
        $height = imagesy( $img ); 
         
        $img_dim = array( $width, $height, $colors ); 
         
        imagedestroy( $img ); 
         
        return $img_dim; 
    } 
     
    /** 
     * Private method returns filetype and size info 
     * 
     * @access private
     * @param  str $src 
     * @return array 
     */ 
    private function get_img_info( $src ) 
    { 
        $curl = curl_init( $src ); 
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );      
        curl_setopt( $curl, CURLOPT_URL, $src ); 
        curl_setopt( $curl, CURLOPT_HEADER, true ); 
        curl_setopt( $curl, CURLOPT_NOBODY, true ); 
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); 
        $data = str_replace( ' ', '', curl_exec( $curl ) ); 
        curl_close( $curl ); 
         
        $size = @explode( 'Content-Length:', $data ); 
         
        //In the event Content-Length is not sent in the header 
        if ( array_key_exists( 1, $size ) ) 
        { 
            $size = preg_split( '/\s+/', $size[1] ); 
            $filesize = (int)$size[0]; 
        } 
        else     
            $filesize = $this->get_img_size( $src );
             
        $type = @explode( 'Content-Type:', $data ); 
        $type = preg_split( '/\s+/', $type[1] ); 
             
        if ( $type[0] === 'image/gif' ) 
            $ext = 1; 
        elseif ( $type[0] === 'image/jpeg' || $type[0] === 'image/jpg' ) 
            $ext = 2;     
        elseif ( $type[0] === 'image/png' ) 
            $ext = 3;      
        else 
            $ext = 'N/A'; 
                 
        return array( $ext, $type[0], $filesize ); 
    } 
    
    /** 
     * Private method returns filesize if Content-Length not sent in header
     * 
     * @access private
     * @param  str $src 
     * @return int
     */ 
    private function get_img_size( $src ) 
    { 
        $curl = curl_init();
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ); 
        curl_setopt( $curl, CURLOPT_URL, $src );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $curl, CURLOPT_HEADER, false );
        $data = curl_exec( $curl );
        curl_close( $curl );
                 
        return strlen( $data );
    } 
} 
$img = new img_info;

print_r( $img->getimagesize( 'http://upload.wikimedia.org/wikipedia/commons/0/04/Labrys-symbol-transparent.png' ) );

//displays: Array( [0] => 493 [1] => 600 [2] => 3 [3] => width="493" height="600" [colors] => 0 [mime] => image/png [size] => 9247 )
/* EOF img_info.php */ 
/* Location: ./img_info.php */ 

转载请注明:谷谷点程序 » php获取远程图片的大小宽度高度