如何从给定的链接中获取图像缩略图


How to get image thumbnail from given link?

在Facebook上共享链接时,Facebook会从给定的链接中获得标题、元描述和图像。

我不知道他们是怎么得到这张照片的。我也在网上搜索,但找不到路。

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

好吧,使用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;
}