php 远程图片大小,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 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值