php實(shí)現(xiàn)高效獲取圖片尺寸的方法
本文實(shí)例講述了php實(shí)現(xiàn)高效獲取圖片尺寸的方法。分享給大家供大家參考。具體分析如下:
php 獲取圖片尺寸的方法我們可以使用 getimagesize 獲取圖片尺寸,但是效率是很低的,首先需要獲取整個(gè)的圖片信息,然后再進(jìn)行操作,下面的例子更科學(xué)算法更好,我們一起來(lái)看看吧.
方法可以用于快速獲取圖片尺寸信息,獲取JPEG格式圖片的尺寸信息,并且不需要下載讀取整個(gè)圖片,經(jīng)測(cè)試這個(gè)函數(shù)不是對(duì)所有JPEG格式的圖片都有效.
1.獲取JPEG格式圖片的尺寸信息,代碼如下:
/*
* http://www.dbjr.com.cn
*/
// 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.實(shí)例代碼如下:
$image_content = file_get_contents($url);
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);
echo $width.'*'.$height."nr";
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- PHP Imagick完美實(shí)現(xiàn)圖片裁切、生成縮略圖、添加水印
- php使用Imagick生成圖片的方法
- PHP中使用imagick實(shí)現(xiàn)把PDF轉(zhuǎn)成圖片
- PHP中使用Imagick實(shí)現(xiàn)各種圖片效果實(shí)例
- php_imagick實(shí)現(xiàn)圖片剪切、旋轉(zhuǎn)、銳化、減色或增加特效的方法
- php Imagick獲取圖片RGB顏色值
- PHP基于php_imagick_st-Q8.dll實(shí)現(xiàn)JPG合成GIF圖片的方法
- php使用imagick模塊實(shí)現(xiàn)圖片縮放、裁剪、壓縮示例
- php獲取圖片信息的方法詳解
- PHP實(shí)現(xiàn)獲取圖片顏色值的方法
- PHP編程獲取圖片的主色調(diào)的方法【基于Imagick擴(kuò)展】
相關(guān)文章
Session的工作機(jī)制詳解和安全性問(wèn)題(PHP實(shí)例講解)
有一點(diǎn)我們必須承認(rèn),大多數(shù)web應(yīng)用程序都離不開(kāi)session的使用。這篇文章將會(huì)結(jié)合php以及http協(xié)議來(lái)分析如何建立一個(gè)安全的會(huì)話(huà)管理機(jī)制2014-04-04有道搜索和IP138的IP的API接口(PHP應(yīng)用)
原理就是通過(guò)php模擬瀏覽器獲取ip地址歸屬地,需要的朋友可以參考下2012-11-11php is_executable判斷給定文件名是否可執(zhí)行實(shí)例
這篇文章主要介紹了php is_executable判斷給定文件名是否可執(zhí)行實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09