简体   繁体   English

如何从PHP上传URL中的图像

[英]How can I upload an image from a URL in PHP

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. 在使用GD或imagemagick的PHP中我如何从URL上传照片,我想构建一个我可以通过几个参数传递并上传图像的函数,我可以将一个大图像上传,将其调整为较小,然后调整一些关闭它的更多缩略图并将所有位置保存到1个图像的位置但是我想添加从URL获取图像然后在其上运行我的代码以调整大小和制作拇指的功能。

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
}

The @ on both functions are there to prevent PHP from throwing errors if the URL is not a valid image. 如果URL不是有效图像,则两个函数上的@用于防止PHP抛出错误。

Depending on your PHP configuration, fopen may or may not allow for it directly: http://php.net/manual/en/function.fopen.php 根据您的PHP配置,fopen可能会也可能不会直接允许它: http//php.net/manual/en/function.fopen.php

Alternatively, you can open a socket ( http://php.net/manual/en/book.sockets.php ) and write / read HTTP ( http://www.faqs.org/rfcs/rfc2616.html ) directly. 或者,您可以直接打开套接字( http://php.net/manual/en/book.sockets.php )和写入/读取HTTP( http://www.faqs.org/rfcs/rfc2616.html )。 I wouldn't use curl unless you're VERY careful about permissions (especially execute), or can guarantee noone malicious will have access to the tool, as you'll effectively open a potential avenue of attack (well, strictly speaking, you already are, but this has a few different ways it can be abused) 除非你非常小心权限(特别是执行),否则我不会使用curl,或者可以保证没有恶意的人可以访问该工具,因为你将有效地打开一个潜在的攻击途径(严格来说,你已经是,但这有几种不同的方式可以滥用)

$img = '';
$fp = fopen($url, 'rb');
if($fp) {
    while($buf = fread($fp, 1024)) {
        $img .= $buf;
    }    
}
fclose($fp);

// assuming url fopen wrappers are enabled //假设启用了url fopen包装器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM