PHP - 从网址创建缩略图


PHP - create thumbnail image from url

我需要一种方法(使用内置的 PHP 库(从 URL 中提取图像并将其保存到本地磁盘,并将 WIDTH 调整为大小/缩放到 150px。我一直在尝试这个:

从上传的图像创建缩略图

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

这看起来很有前途,但我无法弄清楚如何使用它,然后它是否会做我想做的事。关注点/需求:

  1. 这些参数是什么意思:($updir, $img, $id) -- $updir文件的位置吗?还是我希望保存它的位置?$img是文件句柄还是文件名?还是文件的新名称?$id有什么用?作者没有给出任何示例用法。
  2. 该函数似乎需要我指定宽度和高度。我只想指定宽度并让高度成比例。
  3. 我需要通过 2 种不同类型的 URL 拉取图像:(1( 基本 http://foo...,以及 (2( 数据语法:data:image/jpeg;base64,/9j/4AAQ....
  4. 理想情况下,
  5. 我想在所有情况下将这些图像转换为压缩的 jpeg 缩略图(150px 宽度,任何高度(,即使原始图像不是 jpg 格式。

实现这一目标的最佳方法是什么?

只需这样做:

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
}

像这样使用它:

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

这里的诀窍是使用PHP的内部自动文件检测来接收流,这在ImageCreateFromString处理得很好

您可以使用此 SimpleImage 类。您可以直接从 url 调整图像大小并将其保存在本地,如下所示。

$image = new SimpleImage(); 
$image->load('http://website.com/yourimage.jpg'); 
$image->resize(250,400);
$image->save('filename.jpg');

更多示例可以在这里找到。

这些参数是什么意思:($updir、$img、$id(-- $updir文件的位置吗?还是我希望保存它的位置?$img是文件句柄还是文件名?还是文件的新名称?$id有什么用?作者没有给出任何示例用法。

  • $updir似乎是服务器上文件的路径
  • $img是图像文件名(扩展名(
  • $id似乎是文件名的一些数字前缀(似乎纯粹是为了此功能的创建者(。我会在函数标头中将其更改为$id = ''
    .

该函数似乎需要我指定宽度和高度。我只想指定宽度并让高度成比例。

好吧,你可以使用一些数学。如果你想要4:3比例的图像,你可以做一些"比例方程"(不知道它是否有英文名称(:

$thumbnail_width = 1234;
$thumbnail_height = $thumbnail_width * 3 / 4;

但是如果你不知道比例,只想保留比例,那么我认为这个函数对你没有帮助。您可以尝试SimpleImage类,如其他答案中所述(它有resizeToWidth()方法(
.

我需要通过 2 种不同类型的 URL 拉取图像:(1( 基本 http://foo...,以及 (2( 数据语法:data:image/jpeg;base64,/9j/4AAQ。

好吧,这取决于你。你在这里发布了一个函数,你将作为参数传递的内容完全是无关紧要的。至于base64编码的图像,请查看本主题以了解如何将其转换为图像(您可以使用file_put_content()保存图像而不是回显图像(。
,

理想情况下,

我想在所有情况下将这些图像转换为压缩的 jpeg 缩略图(150px 宽度,任何高度(,即使原始图像不是 jpg 格式。

同样,您发布的函数完全适用于输入图像,并且无法转换不同类型的图像。同样,我建议使用SimpleImage类。

这是将 url 转换为图像的一个很好的例子。这似乎有点解决,但它在;)对于imagemagick,如果你使用Windows,你需要设置imagemagick和show imagemagick dll到php。点击这里

我试图通过几个步骤来解释它。

1. getImageSize($name) - 它需要图像名称才能获取图像。因此,您需要提供绝对网址以及带有扩展名的确切图像名称。

例如:"abc.com/edit/9/xyz.png"

在上述情况下,根据您的函数参数。

  1. abc.com/edit/是您的默认网址。
  2. $id您的 ID
  3. 特定记录。 "_"将在您的网址中附加"/"。
  4. $img是特定记录的图像名称。

2,上面的函数会给你一个数组,其中前两个参数是传入函数的图像的高度和宽度。 所以你需要把它存储在变量中。 $height$width.

3,接下来它将检查高度和宽度,如果高度或宽度不符合要求,则需要相应地设置。

4,然后在代码中,接下来是dest_x和dest_y,这将帮助您指向图像的位置。

对于两个不同的 url,您可以将这些 url 存储在常量变量中,并将此常量变量放入您的 getImageSize() 函数中。