【问题标题】:How to get image thumbnail from given link?如何从给定的链接获取图像缩略图?
【发布时间】:2014-11-04 05:54:21
【问题描述】:

在 Facebook 上分享链接时,Facebook 从给定链接中获取标题、元描述和图片。

我不知道他们是如何获得图像的。我也在网上搜索,但我找不到方法。

如何从给定的 URL 获取图片缩略图?

【问题讨论】:

  • 据我所知,它们会在页面上获得第一个大(以防止图标解析)图像。
  • 很可能是 file_get_contents,cURL。

标签: php


【解决方案1】:

好吧,使用 file_put_contents() 将文件保存到您的服务器,然后您可以创建缩略图。您不能直接从 url 获取缩略图。

像这样保存文件:

$url = 'http://example.com/image.ext';
$img = 'yourImgNameOrWithPath.ext';
file_put_contents($img, file_get_contents($url));

使用以下代码创建缩略图:

function createThum($filename,$thumb_width,$thumb_height,$destination)
{
    $my_input_file  = $filename;
    $my_output_file = $destination;
    $jpeg_quality   = 100;
    $size           = getimagesize($my_input_file);
    //$thumb_width  = ($size[0] / $size[1]) * $thumb_height;
    $src_img        = imagecreatefromjpeg($my_input_file);
    $dst_img        = imagecreatetruecolor($thumb_width,$thumb_height);
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]);
    imagejpeg($dst_img, $my_output_file, $jpeg_quality);
    imagedestroy($src_img);
    imagedestroy($dst_img);
    return true;
}

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多