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 urlpreg_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).
- - , , , , , ... !
** : :
$images = array('http://blah.com/1.jpg', 'http://blah.com/2.jpg');
$image_sizes = $this->image_sizes($images);
for ($i = 0; $i < count($image_sizes); $i++) {
if ($image_sizes[$i][0] >= $min || $image_sizes[$i][1] >= $min) {
$dimensions = $this->resize_dimensions($scale_width, $scale_height, $image_sizes[$i][0], $image_sizes[$i][1]);
$img[] = array($images[$i], $dimensions['width'], $dimensions['height']);
}
}
foreach ($img as $image) echo '<img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'"><br><br>';
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;
}
public function resize_dimensions($goal_width, $goal_height, $width, $height) {
$return = array('width' => $width, 'height' => $height);
if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
$return['width'] = floor($goal_width);
$return['height'] = floor($goal_width/$width * $height);
}
else if ($height > $goal_height) {
$return['width'] = floor($goal_height/$height * $width);
$return['height'] = floor($goal_height);
}
return $return;
}