php实现高效获取图片尺寸的方法
程序员文章站
2022-06-11 16:06:13
本文实例讲述了php实现高效获取图片尺寸的方法。分享给大家供大家参考。具体分析如下:
php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸,但是...
本文实例讲述了php实现高效获取图片尺寸的方法。分享给大家供大家参考。具体分析如下:
php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸,但是效率是很低的,首先需要获取整个的图片信息,然后再进行操作,下面的例子更科学算法更好,我们一起来看看吧.
方法可以用于快速获取图片尺寸信息,获取jpeg格式图片的尺寸信息,并且不需要下载读取整个图片,经测试这个函数不是对所有jpeg格式的图片都有效.
1.获取jpeg格式图片的尺寸信息,代码如下:
复制代码 代码如下:
<?php
/*
* //www.jb51.net
*/
// retrieve jpeg width and height without downloading/reading entire image.
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("invalid file stream.");
$new_block = null;
if(!feof($handle)) {
$new_block = fread($handle, 32);
$i = 0;
if($new_block[$i]=="xff" && $new_block[$i+1]=="xd8" && $new_block[$i+2]=="xff" && $new_block[$i+3]=="xe0") {
$i += 4;
if($new_block[$i+2]=="x4a" && $new_block[$i+3]=="x46" && $new_block[$i+4]=="x49" && $new_block[$i+5]=="x46" && $new_block[$i+6]=="x00") {
// read block size and skip ahead to begin cycling through blocks in search of sof marker
$block_size = unpack("h*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
while(!feof($handle)) {
$i += $block_size;
$new_block .= fread($handle, $block_size);
if($new_block[$i]=="xff") {
// new block detected, check for sof marker
$sof_marker = array("xc0", "xc1", "xc2", "xc3", "xc5", "xc6", "xc7", "xc8", "xc9", "xca", "xcb", "xcd", "xce", "xcf");
if(in_array($new_block[$i+1], $sof_marker)) {
// sof marker detected. width and height information is contained in bytes 4-7 after this byte.
$size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
$unpacked = unpack("h*", $size_data);
$unpacked = $unpacked[1];
$height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
$width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
return array($width, $height);
} else {
// skip block marker and read block size
$i += 2;
$block_size = unpack("h*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return false;
}
}
}
}
}
return false;
}
?>
/*
* //www.jb51.net
*/
// retrieve jpeg width and height without downloading/reading entire image.
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("invalid file stream.");
$new_block = null;
if(!feof($handle)) {
$new_block = fread($handle, 32);
$i = 0;
if($new_block[$i]=="xff" && $new_block[$i+1]=="xd8" && $new_block[$i+2]=="xff" && $new_block[$i+3]=="xe0") {
$i += 4;
if($new_block[$i+2]=="x4a" && $new_block[$i+3]=="x46" && $new_block[$i+4]=="x49" && $new_block[$i+5]=="x46" && $new_block[$i+6]=="x00") {
// read block size and skip ahead to begin cycling through blocks in search of sof marker
$block_size = unpack("h*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
while(!feof($handle)) {
$i += $block_size;
$new_block .= fread($handle, $block_size);
if($new_block[$i]=="xff") {
// new block detected, check for sof marker
$sof_marker = array("xc0", "xc1", "xc2", "xc3", "xc5", "xc6", "xc7", "xc8", "xc9", "xca", "xcb", "xcd", "xce", "xcf");
if(in_array($new_block[$i+1], $sof_marker)) {
// sof marker detected. width and height information is contained in bytes 4-7 after this byte.
$size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
$unpacked = unpack("h*", $size_data);
$unpacked = $unpacked[1];
$height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
$width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
return array($width, $height);
} else {
// skip block marker and read block size
$i += 2;
$block_size = unpack("h*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return false;
}
}
}
}
}
return false;
}
?>
2.实例代码如下:
复制代码 代码如下:
$url='http://www.xxxx.com/images/1331189004_28093400.jpg';
$image_content = file_get_contents($url);
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);
echo $width.'*'.$height."nr";
$image_content = file_get_contents($url);
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);
echo $width.'*'.$height."nr";
希望本文所述对大家的php程序设计有所帮助。
上一篇: ai怎么绘制一个简单的卡通小鱼?