【问题标题】:upload external image and save it on my server上传外部图像并将其保存在我的服务器上
【发布时间】:2011-08-15 20:44:54
【问题描述】:

我希望能够上传图片或仅粘贴图片的 URL,以便为我的网站用户上传 sa 个人资料图片。

关键是我不想存储网址,但我想在我的服务器上拥有该图像的副本,因为如果该外部图像丢失,我也不想丢失它...

我相信 facebook 和 tumblr 等会这样做... php 脚本或最佳实践是什么?

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    您可以使用 PHP 函数 (http://php.net/manual/en/function.file-get-contents.php) 从图像中获取内容(字节)

    $contents = file_get_contents('http://www.google.com/images/logos/ps_logo2.png');
    

    您也可以使用 CURL 库...这是一个示例,说明如何下载 ImageFromUrl 并将其保存在本地 SaveLocation 中

    function downloadImageFromUrl($imageLinkURL, $saveLocationPath) {
       $channel = curl_init();
       $curl_setopt($channel, CURLOPT_URL, $imageLinkURL);
       $curl_setopt($channel, CURLOPT_POST, 0);
       $curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
       $fileBytes = curl_exec($channel); 
       curl_close($channel);
    
       $fileWritter = fopen($saveLocationPath, 'w');
       fwrite($fileWritter, $fileBytes);
       fclose($fileWritter); 
    }
    

    您可以按如下方式使用它:

    downloadImageFromUrl("http://www.google.com/images/logos/ps_logo2.png", "/tmp/ps_logo2.png")
    

    你也可以通过解析URL来得到同名的图片……

    【讨论】:

      【解决方案2】:

      我想这就是你要找的:Copy Image from Remote Server Over HTTP

      【讨论】:

        【解决方案3】:

        您可以使用名为imagecreatefromjpeg 的函数或其他东西。它获取图像的 URL 路径并从中创建一个新图像。看看http://php.net/manual/en/function.imagecreatefromjpeg.php

        不过,不同的扩展有不同的功能(如果您更喜欢使用这些功能)。您可能需要从 URL 检查图像扩展名并使用我想的适当功能。

        【讨论】:

          【解决方案4】:

          处理上传已涵盖in this documentation,如果用户粘贴 URL,我建议使用 file_get_contents 将图像的副本保存到您的服务器,然后您可以简单地存储该图像的路径,而不是比外部图像。

          【讨论】:

            猜你喜欢
            • 2012-11-17
            • 1970-01-01
            • 2014-12-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-02
            • 1970-01-01
            相关资源
            最近更新 更多