如何从PHP上传URL中的图像

问题描述:

在使用GD或imagemagick的PHP中,如何从URL上传照片,我想构建一个我可以通过几个参数传递并上传图像的函数,我可以将一个大图像上传,将其调整得更小,然后调整一些缩略图并将其全部保存到1个图像的位置,但我想添加从URL获取图像然后在其上运行我的代码以调整大小和制作拇指的功能。

In PHP using GD or imagemagick how can I uplaod a photo from a URL, I want to build a function that I can pass in a few parameters and uplaod the image, I can currentyl uplaod a big image, resize it smaller, then resize some more thumbnails off it and save all into there locations from 1 image but I would like to add the ability to get an image from a URL and then run my code on it to resize and make thumbs.

我会使用curl或其他任何例子或想法会非常感激

Would I use curl or something else any example or ideas would be greatly appreciated

$image = @ImageCreateFromString(@file_get_contents($imageURL));

if (is_resource($image) === true)
{
    // image is valid, do your magic here
}

else
{
    // not a valid image, show error
}

这两个函数的 @ 用于防止PHP在URL不是有效图像时抛出错误。

The @ on both functions are there to prevent PHP from throwing errors if the URL is not a valid image.