This article mainly introduces how php can efficiently obtain the image size. It is a very practical technique to quickly and efficiently obtain the image size, for more information about how to use php to efficiently obtain image sizes, see the following example. Share it with you for your reference. The specific analysis is as follows:
For php to obtain the image size, we can use getimagesize to obtain the image size, but the efficiency is very low. First, we need to obtain the entire image information and then perform operations, the example below is more scientific and better algorithm. Let's take a look.
The method can be used to quickly obtain the image size information and obtain the size information of JPEG images without the need to download and read the entire image. This function is not effective for all JPEG images.
1. Obtain the size information of JPEG images. The Code is as follows:
The Code is as follows:
<? Php
/*
* Http://www.php.net
*/
// Retrieve JPEG width and height without downloading/reading entire image.
Function get1_size ($ 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 matching ing 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. The instance code is as follows:
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 help you with PHP programming.