PHP _php Example of how to achieve efficient image size acquisition

Source: Internet
Author: User
In this paper, we describe the method of PHP to achieve efficient image size acquisition. Share to everyone for your reference. The specific analysis is as follows:

PHP Get Image size method we can use getimagesize to get the image size, but the efficiency is very low, first need to get the entire picture information, and then to operate, the following example more scientific algorithm better, let's take a look at it.

Method can be used to quickly get picture size information, get the size information of JPEG format picture, and do not need to download to read the entire picture, tested this function is not valid for all JPEG format pictures.

1. Get the size information of the JPEG format picture, the code is as follows:
Copy CodeThe code is as follows: <?php
/*
* Http://www.php.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 [ten]. $unpacked [one]. $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 example code is as follows:
Copy CodeThe 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 is helpful to everyone's PHP programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.