Home >Backend Development >PHP Tutorial >PHP method to efficiently obtain image size, _PHP tutorial

PHP method to efficiently obtain image size, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:471002browse

How to efficiently obtain image size in php,

The example in this article describes the method of efficiently obtaining image size in PHP. Share it with everyone for your reference. The specific analysis is as follows:

How to get the image size in php. We can use getimagesize to get the image size, but the efficiency is very low. First, we need to get the entire image information, and then perform the operation. The following example is more scientific and the algorithm is better. Let’s take a look at it together. Let’s see.

The method can be used to quickly obtain image size information and obtain the size information of JPEG format images without downloading and reading the entire image. After testing, this function is not effective for all JPEG format images.

1. Get the size information of the JPEG format image, the code is as follows:

Copy code The code is as follows:
/*
* http://www.bkjia.com
*/
 
// 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
 
                                                      $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
                                                                                                      }                               } else {
Return FALSE;
                                                                                                                                                                                                                                                                                                                                                       }
}
Return FALSE;
}
?>

2. The example code is as follows:


Copy code

The code is as follows:
$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";


I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/926873.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/926873.htmlTechArticleHow to obtain image size efficiently in php. This article describes the method of obtaining image size efficiently in php. Share it with everyone for your reference. The specific analysis is as follows: php gets pictures...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn