从PHP URL保存图像

从PHP URL保存图像,php,image,Php,Image,我需要保存一个图片从PHP网址到我的电脑。 假设我有一个页面,http://example.com/image.php,拿着一个“花”的图像,没有别的。如何使用新名称(使用PHP)从URL保存此图像?如果将allow\u URL\u fopen设置为true: $url = 'http://example.com/image.php'; $img = '/my/folder/flower.gif'; file_put_contents($img, file_get_contents($url))

我需要保存一个图片从PHP网址到我的电脑。
假设我有一个页面,
http://example.com/image.php
,拿着一个“花”的图像,没有别的。如何使用新名称(使用PHP)从URL保存此图像?

如果将
allow\u URL\u fopen
设置为
true

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
其他用途:


现在,示例将远程图像保存到image.jpg

function save_image($inPath,$outPath)
{ //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

save_image('http://www.someimagesite.com/img.jpg','image.jpg');

我无法获得任何其他解决方案,但我能够使用wget:

$tempDir = '/download/file/here';
$finalDir = '/keep/file/here';
$imageUrl = 'http://www.example.com/image.jpg';

exec("cd $tempDir && wget --quiet $imageUrl");

if (!file_exists("$tempDir/image.jpg")) {
    throw new Exception('Failed while trying to download image');
}

if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) {
    throw new Exception('Failed while trying to move image file from temp dir to final dir');
}

创建一个名为images的文件夹,该文件夹位于您计划放置要创建的php脚本的路径中。确保它对每个人都有写权限,否则脚本将无法工作(它将无法将文件上载到目录)

对我不起作用。确实如此,但由于我的特殊问题,情况略有改善

例如,

当服务器上有重定向时(如您试图保存facebook个人资料图像时),您需要设置以下选项:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

完整解决方案变为:

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
见:


在服务器上安装wkhtmltoimage,然后使用我的软件包packagist.org/packages/tohidhabiby/htmltoimage从目标的url生成一个图像。

这里没有一个答案提到可以压缩url图像(gzip),在这种情况下它们都不起作用

有两种解决方案可以帮助您解决此问题:

第一种方法是使用cURL方法并设置cURL_setopt
CURLOPT\u编码“,

它可以工作,但是从数百个不同图像(png、jpg、ico、gif、svg)的测试来看,它不是最可靠的方法

最有效的方法是检测图像url是否具有内容编码(例如gzip):



有关gzdecode的更多信息。到目前为止,这一切都很好。如果有什么可以做得更好的,请在下面的评论中告诉我们。

页面上有一个动画gif图像。文件以flower.gif的形式存储在文件夹中。但它是空白的。没有图像显示。有任何解决方案吗?打开错误报告(E|u ALL | E|u STRICT)并检查文件的返回值(E|u get_contents()),然后您应该会得到一条合理的错误消息。也许网站管理员已禁止外部推荐。在这种情况下,您可以尝试stream_context_create()并设置适当的HTTP头。urlencode(')==“http%3A%2F%2Fexample.com%2Fimage.php”,显然不是您想要的。文件也是二进制文件,需要设置正确的标志。旧线程的位。。。但不要忘记您要保存到的目录的文件权限。只是浪费了十分钟忘记了显而易见的。谢谢兄弟,你的代码帮我解决了这个问题。但是你能帮我使脚本自动化吗?我的意思是当一个新的gif图像到达url(“”)时,我们的脚本会自动获取新图像并将其存储到我的目录中?你怎么知道,新图像“来了”?我认为riad意味着使用一个包含图像URL的
$\u GET
变量
http://example.com/fetch-image.php?url=http://blabla.com/flower.jpg
。在本例中,您可以在PHP脚本中调用
$\u GET['url']
,如下所示:
$ch=curl\u init($\u GET['url'])
+1是我见过的唯一包含“b”表示二进制标志的答案。@vartec:因为它在抽烟,脸上挂着大大的笑容:)伙计们的url是。请注意,这是一个php生成的图像,而不是一个简单的jpeg。图像或文件扩展名的生成与这个问题有什么关系?fopen需要allow\u url\u fopen=1too@SamThompson从中,它表示块大小(通常为8192)。AFAIK fread可能返回比请求的8K短的块。你不需要计算fwrite的有效块长度吗?非常优雅(需要
允许\u url\u fopen
)。请忽略我的评论,此功能在透明性方面非常有效。我将标题硬编码为image/jpeg。无法建立连接,因为目标计算机主动拒绝它:(如果目标文件夹不存在,它会自动创建吗?@Monnster,不,它不会用于普通文件系统。谢谢zuul,真的&stoe,在经过更多搜索或花费时间之后,发现它真的很有帮助-这帮了大忙!应该在Vartecs答案中注明“需要允许”\u url\u fopen=oni如果复制大量或大的文件,请注意CURL方法更可取(如中的第二个示例),因为CURL大约需要三分之一的时间作为
文件内容
等。此解决方案也是唯一对我有效的解决方案。谢谢Andrew!这是创建网站的图像,而不是在本地保存远程图像
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$img_file='http://www.somedomain.com/someimage.jpg'

$img_file=file_get_contents($img_file);

$file_loc=$_SERVER['DOCUMENT_ROOT'].'/some_dir/test.jpg';

$file_handler=fopen($file_loc,'w');

if(fwrite($file_handler,$img_file)==false){
    echo 'error';
}

fclose($file_handler);
copy('http://example.com/image.php', 'local/folder/flower.jpg');
$url    = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg';
$img    = 'miki.png';
$file   = file($url);
$result = file_put_contents($img, $file)
$data = file_get_contents('http://example.com/image.php');
$img = imagecreatefromstring($data);
imagepng($img, 'test.png');
// ... image validation ...

// Handle compression & redirection automatically
$ch = curl_init($image_url);
$fp = fopen($dest_path, 'wb');

curl_setopt($ch, CURLOPT_FILE, $fp);
// Exclude header data
curl_setopt($ch, CURLOPT_HEADER, 0);
// Follow redirected location
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Auto detect decoding of the response | identity, deflate, & gzip
curl_setopt($ch, CURLOPT_ENCODING, '');

curl_exec($ch);

curl_close($ch);
fclose($fp);
// ... image validation ...

// Fetch all headers from URL
$data = get_headers($image_url, true);

// Check if content encoding is set
$content_encoding = isset($data['Content-Encoding']) ? $data['Content-Encoding'] : null;

// Set gzip decode flag
$gzip_decode = ($content_encoding == 'gzip') ? true : false;

if ($gzip_decode)
{
    // Get contents and use gzdecode to "unzip" data
    file_put_contents($dest_path, gzdecode(file_get_contents($image_url)));
}
else
{
    // Use copy method
    copy($image_url, $dest_path);
}