Home >Backend Development >PHP Tutorial >How can I speed up remote image dimension extraction with PHP\'s getimagesize function?
Accelerating getimagesize in PHP for Remote Image Dimensions Extraction
Determining the dimensions of remote images using PHP's getimagesize function can be sluggish for large image sets. This article explores an alternative approach that leverages file_get_contents to retrieve a portion of binary data from images for size analysis.
To extract image dimensions using this method, implement the following function:
<code class="php">function ranger($url) { $headers = ["Range: bytes=0-32768"]; $curl = curl_init($url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); curl_close($curl); return $data; }</code>
Next, execute the following code to retrieve the dimensions of a remote image:
<code class="php">$url = "http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg"; $raw = ranger($url); $im = imagecreatefromstring($raw); $width = imagesx($im); $height = imagesy($im); echo $width." x ".$height." ({$stop}s)";</code>
In our test case, this approach yielded the dimensions of a remote image in under 0.2 seconds.
Note: Adjust the range parameter (in the "Range" header) to retrieve more or less binary data from the image. Experiment with different ranges to optimize performance for your specific application.
The above is the detailed content of How can I speed up remote image dimension extraction with PHP's getimagesize function?. For more information, please follow other related articles on the PHP Chinese website!