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

时间:2014-11-04 05:54:22

标签: php

在Facebook上分享链接时,Facebook会从指定链接获取标题,元描述和图像。

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

如何从指定的网址获取图片缩略图?

1 个答案:

答案 0 :(得分:1)

好吧,使用file_put_contents()将文件保存到服务器,然后就可以创建缩略图了。你无法直接从网址获取缩略图。

像这样保存文件:

$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;
}
相关问题