Saving image from PHP URL

后端 未结 12 1663
日久生厌
日久生厌 2020-11-21 17:10

I need to save an image from a PHP URL to my PC. Let\'s say I have a page, http://example.com/image.php, holding a single \"flower\" image, nothing else. How ca

相关标签:
12条回答
  • 2020-11-21 17:51

    If you have allow_url_fopen set to true:

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

    Else use cURL:

    $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_exec($ch);
    curl_close($ch);
    fclose($fp);
    
    0 讨论(0)
  • 2020-11-21 17:54

    See file()PHP Manual:

    $url    = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg';
    $img    = 'miki.png';
    $file   = file($url);
    $result = file_put_contents($img, $file)
    
    0 讨论(0)
  • 2020-11-21 17:55

    None of the answers here mention the fact that a URL image can be compressed (gzip), and none of them work in this case.

    There are two solutions that can get you around this:

    The first is to use the cURL method and set the curl_setopt CURLOPT_ENCODING, '':

    // ... 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);
    

    It works, but from hundreds of tests of different images (png, jpg, ico, gif, svg), it is not the most reliable way.

    What worked out best is to detect whether an image url has content encoding (e.g. gzip):

    // ... 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);
    }
    

    For more information regarding gzdecode see this thread. So far this works fine. If there's anything that can be done better, let us know in the comments below.

    0 讨论(0)
  • 2020-11-21 17:57
    $content = file_get_contents('http://example.com/image.php');
    file_put_contents('/my/folder/flower.jpg', $content);
    
    0 讨论(0)
  • 2020-11-21 18:01
    $data = file_get_contents('http://example.com/image.php');
    $img = imagecreatefromstring($data);
    imagepng($img, 'test.png');
    
    0 讨论(0)
  • 2020-11-21 18:05

    Create a folder named images located in the path you are planning to place the php script you are about to create. Make sure it has write rights for everybody or the scripts won't work ( it won't be able to upload the files into the directory).

    0 讨论(0)
提交回复
热议问题