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.fzitv.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.實(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)文章
php array_merge_recursive 數(shù)組合并
這篇文章主要介紹了php array_merge_recursive 將兩個(gè)或多個(gè)數(shù)組合并為一個(gè)數(shù)組的相關(guān)資料,需要的朋友可以參考下2016-10-10
linux mint下安裝phpstorm2020包括JDK部分的教程詳解
這篇文章主要介紹了linux mint下安裝phpstorm2020包括JDK部分的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例
這篇文章主要介紹了PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例,本文講解了目標(biāo)、角色、應(yīng)用場(chǎng)景、優(yōu)勢(shì)等內(nèi)容,并給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
ThinkPHP3.1新特性之多數(shù)據(jù)庫(kù)操作更加完善
對(duì)于早期版本的ThinkPHP來(lái)說(shuō),切換數(shù)據(jù)庫(kù)需要使用高級(jí)模型,而現(xiàn)在的3.1版本則可以更加輕松的解決了。這篇文章主要介紹了ThinkPHP3.1對(duì)多數(shù)據(jù)庫(kù)操作,需要的朋友可以參考下2014-06-06
php獲取目錄所有文件并將結(jié)果保存到數(shù)組(實(shí)例)
php讀取目錄文件在平時(shí)的開(kāi)發(fā)中還是經(jīng)常要用到的,這里寫(xiě)個(gè)小例子學(xué)習(xí)一下如何用PHP把目錄下文件列出來(lái)。2013-10-10
laravel 解決Eloquent ORM的save方法無(wú)法插入數(shù)據(jù)的問(wèn)題
今天小編就為大家分享一篇laravel 解決Eloquent ORM的save方法無(wú)法插入數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP+jQuery 注冊(cè)模塊開(kāi)發(fā)詳解
本文主要是記錄了開(kāi)發(fā)PHP+jQuery注冊(cè)模塊的全過(guò)程,包含填寫(xiě)欄目用戶名、郵箱、密碼、重復(fù)密碼、驗(yàn)證碼等,非常的詳細(xì),推薦給大家2014-10-10
PHP中使用substr()截取字符串出現(xiàn)中文亂碼問(wèn)題該怎么辦
本文給大家介紹使用php substr()截取字符串出現(xiàn)亂碼問(wèn)題該怎么辦,涉及到php substr()方法的一些知識(shí)點(diǎn),感興趣的朋友一起學(xué)習(xí)下吧2015-10-10

