php实现高效获取图片尺寸的方法
本篇章节讲解php实现高效获取图片尺寸的方法。分享给大家供大家参考。具体分析如下: php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸,但是效率是很低的,首先需要获取整个的图片信息,然后再进行操作,下面的例子更科学算法更好,我们一起来看看吧. 方法可以用于快速获取图片尺寸信息,获取JPEG格式图片的尺寸信息,并且不需要下载读取整个图片,经测试这个函数不是对所有JPEG格式的图片都有效. 1.获取JPEG格式图片的尺寸信息,代码如下: 代码如下: // 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]); // New block detected,check for SOF marker $sof_marker = array("xC0","xC1","xC2","xC3","xC5","xC6","xC7","xC8","xC9","xCA","xCB","xCD","xCE","xCF"); // 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]; // Skip block marker and read block size $i += 2; 2.实例代码如下: <div class="codetitle"><a style="CURSOR: pointer" data="46181" class="copybut" id="copybut46181" onclick="doCopy('code46181')"> 代码如下:<div class="codebody" id="code46181">$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"; 希望本文所述对大家的PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |