从外部链接选择缩略图 [英] Choosing a thumbnail from an external link

查看:80
本文介绍了从外部链接选择缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个从外部链接检索缩略图列表的脚本,很像Facebook在分享链接时可以选择与该帖子相关联的缩略图。

I am trying to build a script that retrieves a list of thumbnail images from an external link, much like Facebook does when you share a link and can choose the thumbnail image that is associated with that post.

我的脚本目前的工作原理如下:

My script currently works like this:


  • file_get_contents 在URL上

  • preg_match_all 匹配任何< img src =在内容中

  • 将完整的URL作为每个图像并将其存储在数组中

  • 如果存在< 10图像循环并使用 getimagesize 查找宽度和高度

  • 如果有> 10个图像,它会循环使用 fread imagecreatefromstring 查找宽度和高度(为了速度)

  • 一旦所有宽度并且高度被制定出来,它循环遍历,并且仅将图像添加到具有最小宽度和高度的新阵列(因此,仅显示较大的图像,较小的图像不太可能描述URL)

  • 每个图像的新维度都按比例缩小,并被返回...

  • file_get_contents on the URL
  • preg_match_all to match any <img src="" in the contents
  • Works out the full URL to each image and stores it in an array
  • If there are < 10 images it loops through and uses getimagesize to find width and height
  • If there are > 10 images it loops through and uses fread and imagecreatefromstring to find width and height (for speed)
  • Once all width and heights are worked out it loops through and only adds the images to a new array that have a minimum width and height (so only larger images are shown, smaller images are less likely to be descriptive of the URL)
  • Each image has its new dimensions worked out (scaled down proportionally) and are returned...

< img src ='。$ image [0]。width ='。$ image [1]。'height ='$ image [2]。>< br& ;< br>

目前这个工作正常,但是我可能会遇到一些问题:

At the moment this works fine, but there are a number of problems I can potentially have:


  1. SPEED!如果网址上有很多图片,处理

  2. MEMORY将需要更长的时间!使用 getimagesize fread & imagecreatefromstring 将整个图像存储在内存中,页面上的任何大图像都可能会损坏服务器的内存并杀死我的脚本(和服务器)

  1. SPEED! If the URL has many images on the page it will take considerably longer to process
  2. MEMORY! Using getimagesize or fread & imagecreatefromstring will store the whole image in memory, any large images on the page could eat up the server's memory and kill my script (and server)

我找到的一个解决方案是能够从图像的标题中检索图像的宽度和高度,而无需下载整个图像,尽管我只找到了一些可以为JPG做这个代码(需要支持GIF& PNG)。

One solution I have found is being able to retrieve the image width and height from the header of the image without having to download the whole image, though I have only found some code to do this for JPG's (it would need to support GIF & PNG).

任何人都可以提出任何建议来帮助我解决上述问题,或者也许你可以建议另一种做法这样做我可以开放的想法...谢谢!

Can anyone make any suggestions to help me with either problem mentioned above, or perhaps you can suggest another way of doing this I am open to ideas... Thanks!

**编辑:代码如下:

** Code below:

// 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;
}


推荐答案

getimagesize只读标题, imagecreatefromstring读取整个​​图像。由GD,ImageMagick或GraphicsMagic读取的图像作为位图存储,因此消耗宽度高度(3或4)个字节,您无法做到这一点。
您的问题的最佳解决方案是使卷曲多请求(请参阅 http://ru.php.net/manual/en/function.curl-multi-select.php ),然后逐个进程用GD或任何其他库收到图像。为了使内存消耗降低一点,您可以将图像文件存储在磁盘上,而不是内存中。

getimagesize reads only header, but imagecreatefromstring reads whole image. Image read by GD, ImageMagick or GraphicsMagic is stored as bitmap so it consumes widthheight(3 or 4) bytes, and there's nothing you can do about it. The best possible solution for your problem is to make curl multi-request (see http://ru.php.net/manual/en/function.curl-multi-select.php ), and then one by one process recieved images with GD or any other library. And to make memory consumption a bit lower, you can store image files on disk, not in memory.

这篇关于从外部链接选择缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆