如何从PHP上传URL中的图像 [英] How can I upload an image from a URL in PHP

查看:95
本文介绍了如何从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.

这篇关于如何从PHP上传URL中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆