Select thumbnails from external links

I am trying to create a script that retrieves a list of thumbnails from an external link, as Facebook does when you share the link and can select the thumbnail image associated with this post.

Now my script works as follows:

  • file_get_contents in url
  • preg_match_allto match any <img src=""in content
  • Executes the full URL for each image and stores it in an array
  • If there are <10 images it scrolls and uses getimagesizeto search for width and height
  • If there is a> 10 images, they scroll and use fread, and imagecreatefromstringto find the width and height (for speed)
  • After all width and height values ​​have been processed, it moves and adds images to the new array with a minimum width and height (therefore, only larger images are displayed, smaller images are less likely to describe the URL)
  • Each image has its own new dimensions (reduced proportionally) and returns ...

<img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'"><br><br>

This works fine for now, but there are a number of issues that I could potentially have:

  • SPEED! If the URL contains many images on the page, it will take significantly more time to process
  • MEMORY! Using getimagesizeor freadand imagecreatefromstringsaves the entire image in memory, any large images on the page can eat the server’s memory and kill my script (and the server)

, , - , JPG ( GIF PNG).

- - , , , , , ... !

** : :

// Example images array
$images = array('http://blah.com/1.jpg', 'http://blah.com/2.jpg');

// Find the image sizes
$image_sizes = $this->image_sizes($images);

// Find the images that meet the minimum size
for ($i = 0; $i < count($image_sizes); $i++) {
    if ($image_sizes[$i][0] >= $min || $image_sizes[$i][1] >= $min) {                
        // Scale down the original image size
        $dimensions = $this->resize_dimensions($scale_width, $scale_height, $image_sizes[$i][0], $image_sizes[$i][1]);
        $img[] = array($images[$i], $dimensions['width'], $dimensions['height']);
    }
}

// Output the images
foreach ($img as $image) echo '<img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'"><br><br>';

/**
 * Retrieves the image sizes
 * Uses the getimagesize() function or the filesystem for speed increases
 */
public function image_sizes($images) {
    $out = array();
    if (count($images) < 10) {
        foreach ($images as $image) {
            list($width, $height) = @getimagesize($image);
            if (is_numeric($width) && is_numeric($height)) {
                $out[] = array($width, $height);
            }
            else {
                $out[] = array(0, 0);
            }
        }
    }
    else {
        foreach ($images as $image) {
            $handle = @fopen($image, "rb");
            $contents = "";
            if ($handle) {
                while(true) {
                    $data = fread($handle, 8192);
                    if (strlen($data) == 0) break;
                    $contents .= $data;
                }
                fclose($handle);
                $im = @imagecreatefromstring($contents);
                if ($im) {
                    $out[] = array(imagesx($im), imagesy($im));
                }
                else {
                    $out[] = array(0, 0);
                }
                @imagedestroy($im);
            }
            else {
                $out[] = array(0, 0);
            }
        }
    }
    return $out;
}

/**
 * Calculates restricted dimensions with a maximum of $goal_width by $goal_height 
 */
public function resize_dimensions($goal_width, $goal_height, $width, $height) {
    $return = array('width' => $width, 'height' => $height);

    // If the ratio > goal ratio and the width > goal width resize down to goal width
    if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
        $return['width'] = floor($goal_width);
        $return['height'] = floor($goal_width/$width * $height);
    }

    // Otherwise, if the height > goal, resize down to goal height
    else if ($height > $goal_height) {
        $return['width'] = floor($goal_height/$height * $width);
        $return['height'] = floor($goal_height);
    }   
    return $return;
}
+3
2

getimagesize , imagecreatefromstring . , GD, ImageMagick GraphicsMagic , (3 4) , . (. http://ru.php.net/manual/en/function.curl-multi-select.php), GD . , , .

+1

, ( ) - HTML , .

0

Source: https://habr.com/ru/post/1732502/


All Articles