Home > Backend Development > PHP Tutorial > How can I speed up remote image dimension extraction with PHP\'s getimagesize function?

How can I speed up remote image dimension extraction with PHP\'s getimagesize function?

Patricia Arquette
Release: 2024-11-04 01:25:03
Original
458 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template