Sasa1234
Sasa1234

Reputation: 948

How to get image thumbnail from given link?

When sharing link on Facebook, Facebook get Title,Meta Description and Image from given link.

I have no idea how they get the image. I search on the internet as well as, but I could n't find a way.

How to get image thumbnail from given URL?

Upvotes: 2

Views: 3565

Answers (1)

Shreejibawa
Shreejibawa

Reputation: 1868

Well, save file to your server using file_put_contents(), and then you can create thumbnail. you can't get thumbnail directly from url.

Save file like this :

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

Create thumbnail using following code :

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

Upvotes: 1

Related Questions