PHP - 从 url 创建缩略图
PHP - create thumbnail image from url
我需要一种方法(使用内置 PHP 库)从 URL 中下载图像并将其保存到本地磁盘,resized/scaled 宽度为 150px。我一直在尝试这个:
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");
}
}
这看起来很有前途,但我不知道如何使用它,以及它是否会做我想做的事。 Concerns/needs:
- 这些参数是什么意思:
($updir, $img, $id)
-- $updir
是文件的位置吗?或者我想要它保存的位置? $img
是文件句柄还是文件名?还是文件的新名称? $id
有什么用?作者没有给出任何示例用法。
- 这个功能似乎需要我指定宽度和高度。我只想指定WIDTH,让高度成比例。
- 我需要通过 2 种不同类型的 URL 提取图像:(1) 基本 http://foo...,以及 (2) 数据语法:
data:image/jpeg;base64,/9j/4AAQ....
- 理想情况下,我想在所有情况下将这些图像转换为压缩的 jpeg 缩略图(150 像素宽度,任何高度),即使原始图像不是 jpg 格式。
完成此任务的最佳方法是什么?
您可以使用这个 SimpleImage class。您可以直接从 url 调整图像大小并像这样将其保存在本地。
$image = new SimpleImage();
$image->load('http://website.com/yourimage.jpg');
$image->resize(250,400);
$image->save('filename.jpg');
可以找到更多示例 here。
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.
$updir
似乎是服务器上文件的路径
$img
是图像文件名( 带 扩展名)
$id
似乎是文件名的一些数字前缀(似乎纯粹是为了这个函数的创建者)。我会在函数头中将其更改为 $id = ''
。
.
The function seems to require me to specify WIDTH and HEIGHT. I only want to specify WIDTH and let the height be proportional.
好吧,你可以使用一些数学。如果你想要 4:3 比例的图像,你可以做一些 "ratio equation" (不知道它是否有英文名称):
$thumbnail_width = 1234;
$thumbnail_height = $thumbnail_width * 3 / 4;
但是如果你不知道比例,只想保持比例,那我觉得这个功能帮不了你。您可以尝试 SimpleImage
class 正如其他答案中已经说过的那样(它有 resizeToWidth()
方法)
.
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....
好吧,这取决于你。你在这里发布了一个函数,你将作为参数传递的是完全独立的事情。至于 base64
编码图像,检查 this topic 以了解如何将其转换为图像(您可以使用 file_put_content()
保存图像而不是回显它)。
,
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.
同样,您发布的函数完全适用于输入图像,并且不能转换不同的类型。同样,我建议使用 SimpleImage
class.
这是将 url 转换为图像的一个很好的例子。似乎有点变通,但效果很好;)
对于 imagemagick ,如果您使用 windows,则需要设置 imagemagick 并将 imagemagick dll 显示到 php。
Click here
只需这样做:
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
处理得很好
我试图分几步解释它。
1.getImageSize($name)
- 获取图片需要图片名。因此,您需要提供绝对 url 以及带有扩展名的确切图像名称。
例如:"abc.com/edit/9/xyz.png"
在上述情况下,根据您的函数参数。
abc.com/edit/
是您的默认 url。
$id
是你的 的id
- 特定记录。 “_”将在您的 url.
中附加“/”
$img
是特定记录的图像名称。
2,上面的函数会给你一个数组,其中前两个参数是函数传入的图像的高度和宽度。所以你需要将它存储在变量中。 $height
和 $width
。
3、接下来会检查高度和宽度,如果高度或宽度不符合要求,则需要进行相应设置。
4,然后在代码中,接下来是dest_x和dest_y,这将帮助您指向图像的位置。
而对于两个不同的 url,您可以将这些 url 存储在常量变量中,并将此常量变量放入您的 getImageSize()
函数中。
我需要一种方法(使用内置 PHP 库)从 URL 中下载图像并将其保存到本地磁盘,resized/scaled 宽度为 150px。我一直在尝试这个:
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");
}
}
这看起来很有前途,但我不知道如何使用它,以及它是否会做我想做的事。 Concerns/needs:
- 这些参数是什么意思:
($updir, $img, $id)
--$updir
是文件的位置吗?或者我想要它保存的位置?$img
是文件句柄还是文件名?还是文件的新名称?$id
有什么用?作者没有给出任何示例用法。 - 这个功能似乎需要我指定宽度和高度。我只想指定WIDTH,让高度成比例。
- 我需要通过 2 种不同类型的 URL 提取图像:(1) 基本 http://foo...,以及 (2) 数据语法:
data:image/jpeg;base64,/9j/4AAQ....
- 理想情况下,我想在所有情况下将这些图像转换为压缩的 jpeg 缩略图(150 像素宽度,任何高度),即使原始图像不是 jpg 格式。
完成此任务的最佳方法是什么?
您可以使用这个 SimpleImage class。您可以直接从 url 调整图像大小并像这样将其保存在本地。
$image = new SimpleImage();
$image->load('http://website.com/yourimage.jpg');
$image->resize(250,400);
$image->save('filename.jpg');
可以找到更多示例 here。
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.
$updir
似乎是服务器上文件的路径$img
是图像文件名( 带 扩展名)$id
似乎是文件名的一些数字前缀(似乎纯粹是为了这个函数的创建者)。我会在函数头中将其更改为$id = ''
。
.
The function seems to require me to specify WIDTH and HEIGHT. I only want to specify WIDTH and let the height be proportional.
好吧,你可以使用一些数学。如果你想要 4:3 比例的图像,你可以做一些 "ratio equation" (不知道它是否有英文名称):
$thumbnail_width = 1234;
$thumbnail_height = $thumbnail_width * 3 / 4;
但是如果你不知道比例,只想保持比例,那我觉得这个功能帮不了你。您可以尝试 SimpleImage
class 正如其他答案中已经说过的那样(它有 resizeToWidth()
方法)
.
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....
好吧,这取决于你。你在这里发布了一个函数,你将作为参数传递的是完全独立的事情。至于 base64
编码图像,检查 this topic 以了解如何将其转换为图像(您可以使用 file_put_content()
保存图像而不是回显它)。
,
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.
同样,您发布的函数完全适用于输入图像,并且不能转换不同的类型。同样,我建议使用 SimpleImage
class.
这是将 url 转换为图像的一个很好的例子。似乎有点变通,但效果很好;) 对于 imagemagick ,如果您使用 windows,则需要设置 imagemagick 并将 imagemagick dll 显示到 php。 Click here
只需这样做:
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
我试图分几步解释它。
1.getImageSize($name)
- 获取图片需要图片名。因此,您需要提供绝对 url 以及带有扩展名的确切图像名称。
例如:"abc.com/edit/9/xyz.png"
在上述情况下,根据您的函数参数。
abc.com/edit/
是您的默认 url。$id
是你的 的id
- 特定记录。 “_”将在您的 url. 中附加“/”
$img
是特定记录的图像名称。
2,上面的函数会给你一个数组,其中前两个参数是函数传入的图像的高度和宽度。所以你需要将它存储在变量中。 $height
和 $width
。
3、接下来会检查高度和宽度,如果高度或宽度不符合要求,则需要进行相应设置。
4,然后在代码中,接下来是dest_x和dest_y,这将帮助您指向图像的位置。
而对于两个不同的 url,您可以将这些 url 存储在常量变量中,并将此常量变量放入您的 getImageSize()
函数中。