Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中存储链接中的图像_Php_Image_Storage - Fatal编程技术网

在PHP中存储链接中的图像

在PHP中存储链接中的图像,php,image,storage,Php,Image,Storage,给定一个图像的直接链接,如何使用php将实际图像存储在我在服务器上创建的文件夹中 谢谢 在坚果壳中,抓取图像,存储图像。。那很容易 注意:必须将allow_url_fopen php.ini设置设置为true才能使上述示例正常工作 在坚果壳中,抓取图像,存储图像。。那很容易 注意:必须将allow_url_fopen php.ini设置设置为true才能使上述示例正常工作。还有一种非常简单的方法: $img = 'http://www.domain.com/image.jpg'; $img =

给定一个图像的直接链接,如何使用php将实际图像存储在我在服务器上创建的文件夹中

谢谢

在坚果壳中,抓取图像,存储图像。。那很容易

注意:必须将allow_url_fopen php.ini设置设置为true才能使上述示例正常工作

在坚果壳中,抓取图像,存储图像。。那很容易


注意:必须将allow_url_fopen php.ini设置设置为true才能使上述示例正常工作。

还有一种非常简单的方法:

$img = 'http://www.domain.com/image.jpg';
$img = imagecreatefromjpeg($img);
$path = '/local/absolute/path/images/';
imagejpeg($img, $path);
上述关于allow_url_fopen的评论也适用于此方法。如果您有一个相当严格的主机,则可能需要利用cURL来解决此问题:

/**
 * For when allow_url_fopen is closed.
 * 
 * @param  string  $img       The web url to the image you wish to dl
 * @param  string  $fullpath  The local absolute path where you want to save the img
 */
function save_image($img, $fullpath) {
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $binary_img = curl_exec($ch);
    curl_close ($ch);
    if (file_exists($fullpath)){
        unlink($fullpath);
    }

    $fp = fopen($fullpath, 'x');
    fwrite($fp, $binary_img);
    fclose($fp);
}

还有一种非常简单的方法:

$img = 'http://www.domain.com/image.jpg';
$img = imagecreatefromjpeg($img);
$path = '/local/absolute/path/images/';
imagejpeg($img, $path);
上述关于allow_url_fopen的评论也适用于此方法。如果您有一个相当严格的主机,则可能需要利用cURL来解决此问题:

/**
 * For when allow_url_fopen is closed.
 * 
 * @param  string  $img       The web url to the image you wish to dl
 * @param  string  $fullpath  The local absolute path where you want to save the img
 */
function save_image($img, $fullpath) {
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $binary_img = curl_exec($ch);
    curl_close ($ch);
    if (file_exists($fullpath)){
        unlink($fullpath);
    }

    $fp = fopen($fullpath, 'x');
    fwrite($fp, $binary_img);
    fclose($fp);
}

是的,假设在php.ini中设置了allow_url_fopen,这将起作用。如果不是,那么您必须使用CURL来获取图像,然后使用file_put_contents()来编写它。是的,假设在php.ini中设置了allow_url_fopen,那么这将起作用。如果不是,则必须使用CURL来获取图像,然后使用file_put_contents()来编写图像。您建议的第一种方法将比简单地复制图像需要更多的开销。唉,它允许您使用一些GD技巧来操纵图像。您建议的第一种方法将比简单地向下复制图像涉及更多的开销。唉,它允许您使用一些GD技巧来操纵图像。