PHP - create thumbnail image from url

HerrimanCoder

I need a way (using built-in PHP libs) to pull down an image from URL and save it to local disk with resized/scaled WIDTH to 150px. I have been trying this:

Creating a thumbnail from an uploaded image

function makeThumbnails($updir, $img, $id)
{
    $thumbnail_width = 134;
    $thumbnail_height = 189;
    $thumb_beforeword = "thumb";
    $arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];
    if ($original_width > $original_height) {
        $new_width = $thumbnail_width;
        $new_height = intval($original_height * $new_width / $original_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width = intval($original_width * $new_height / $original_height);
    }
    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);
    if ($arr_image_details[2] == 1) {
        $imgt = "ImageGIF";
        $imgcreatefrom = "ImageCreateFromGIF";
    }
    if ($arr_image_details[2] == 2) {
        $imgt = "ImageJPEG";
        $imgcreatefrom = "ImageCreateFromJPEG";
    }
    if ($arr_image_details[2] == 3) {
        $imgt = "ImagePNG";
        $imgcreatefrom = "ImageCreateFromPNG";
    }
    if ($imgt) {
        $old_image = $imgcreatefrom("$updir" . $id . '_' . "$img");
        $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
        $imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img");
    }
}

That looks very promising but I can't figure out how to use it, and then whether it will do what I want. Concerns/needs:

  1. What do these params mean: ($updir, $img, $id) -- Is $updir the location of the file? Or the location where I want it saved? Is $img a file handle or the name of the file? Or the new name of the file? What is $id for? The author didn't give any sample usage.
  2. The function seems to require me to specify WIDTH and HEIGHT. I only want to specify WIDTH and let the height be proportional.
  3. I need to pull images over 2 different types of URL: (1) The basic http://foo..., and (2) the data syntax: data:image/jpeg;base64,/9j/4AAQ....
  4. Ideally I'd like to convert these images in all cases down to a compressed jpeg thumbnail (150px width, any height), even if the original image was not in jpg format.

What's the best way to accomplish this?

flyandi

Just do this:

function Thumbnail($url, $filename, $width = 150, $height = true) {

 // download and create gd image
 $image = ImageCreateFromString(file_get_contents($url));

 // calculate resized ratio
 // Note: if $height is set to TRUE then we automatically calculate the height based on the ratio
 $height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;

 // create image 
 $output = ImageCreateTrueColor($width, $height);
 ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));

 // save image
 ImageJPEG($output, $filename, 95); 

 // return resized image
 return $output; // if you need to use it
}

Use it like this:

Thumbnail("http://cdn.cutestpaw.com/wp-content/uploads/2013/01/l-Cute-Kitten.jpg", "kitten.jpg");

The trick here is to use PHP's internal automatic file detection for the incoming stream which is very good handled by ImageCreateFromString

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create thumbnail from image and insert in SQLite as blob

From Dev

create thumbnail from video URL in C#

From Dev

Create thumbnail from video without ffmpeg in PHP

From Dev

Create thumbnail from video without ffmpeg in PHP

From Dev

Libgdx create image thumbnail

From Dev

Libgdx create image thumbnail

From Dev

Google images: get large image URL from thumbnail URL

From Dev

how to create thumbnail image by using video url for jquery jplayer

From Dev

Create thumbnail in php

From Dev

Create thumbnail from uploaded image in Yii2

From Dev

Create dynamic thumbnail image from drawable resource and set to imageView

From Dev

How to create a thumbnail image from a video in a JavaScript Application

From Dev

how to display youtube thumbnail image from url with Angularjs

From Dev

Get / Make thumbnail image from video url getting in API response

From Dev

PHP create image files from URL with GET attributes

From Dev

Create thumbnail image for PDF in Android

From Dev

Create thumbnail image for PDF in Android

From Dev

Video thumbnail image from directory

From Dev

Video thumbnail image from directory

From Dev

Create thumbnail from video - Xamarin

From Dev

video thumbnail image not generated from local video URL(asset URL) iOS

From Dev

Display thumbnail image but print full_res using generateimage.php?url=

From Dev

php get image from url

From Dev

Display image thumbnail of image selected from filepicker

From Dev

How to create thumbnail of video url form server

From Dev

PHP - Create Thumbnail & maintaining aspect ratio

From Dev

Create thumbnail of PDF or PPT file using php

From Dev

Create square 1:1 thumbnail in PHP

From Dev

How to display post thumbnail with modified url in php?

Related Related

  1. 1

    Create thumbnail from image and insert in SQLite as blob

  2. 2

    create thumbnail from video URL in C#

  3. 3

    Create thumbnail from video without ffmpeg in PHP

  4. 4

    Create thumbnail from video without ffmpeg in PHP

  5. 5

    Libgdx create image thumbnail

  6. 6

    Libgdx create image thumbnail

  7. 7

    Google images: get large image URL from thumbnail URL

  8. 8

    how to create thumbnail image by using video url for jquery jplayer

  9. 9

    Create thumbnail in php

  10. 10

    Create thumbnail from uploaded image in Yii2

  11. 11

    Create dynamic thumbnail image from drawable resource and set to imageView

  12. 12

    How to create a thumbnail image from a video in a JavaScript Application

  13. 13

    how to display youtube thumbnail image from url with Angularjs

  14. 14

    Get / Make thumbnail image from video url getting in API response

  15. 15

    PHP create image files from URL with GET attributes

  16. 16

    Create thumbnail image for PDF in Android

  17. 17

    Create thumbnail image for PDF in Android

  18. 18

    Video thumbnail image from directory

  19. 19

    Video thumbnail image from directory

  20. 20

    Create thumbnail from video - Xamarin

  21. 21

    video thumbnail image not generated from local video URL(asset URL) iOS

  22. 22

    Display thumbnail image but print full_res using generateimage.php?url=

  23. 23

    php get image from url

  24. 24

    Display image thumbnail of image selected from filepicker

  25. 25

    How to create thumbnail of video url form server

  26. 26

    PHP - Create Thumbnail & maintaining aspect ratio

  27. 27

    Create thumbnail of PDF or PPT file using php

  28. 28

    Create square 1:1 thumbnail in PHP

  29. 29

    How to display post thumbnail with modified url in php?

HotTag

Archive