Asked  7 Months ago    Answers:  5   Viewed   36 times

I need to save an image from a url using CURL and save it to a folder on my server. I've been battling with this code to no avail. Ideally I'd like to grab the image and save it as "photo1" or something. Help!

    function GetImageFromUrl($link)

    {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 0);

    curl_setopt($ch,CURLOPT_URL,$link);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result=curl_exec($ch);

    curl_close($ch);

    return $result;

    }

    $sourcecode = GetImageFromUrl($iticon);

    $savefile = fopen(' /img/uploads/' . $iconfilename, 'w');
    fwrite($savefile, $sourcecode);
    fclose($savefile);

 Answers

12

try this:

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

and ensure that in php.ini allow_url_fopen is enable

Wednesday, March 31, 2021
 
Precastic
answered 7 Months ago
100

Sorry, I'm not sure what is you want.

This is the example to get from a image url, echo header and save image to a file.

But, if you want a proxy, you should use web server (Nginx, Apache, etc), PHP is no need

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://img3.cache.netease.com/www/logo/logo_png.png");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.163.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);

list($header, $image) = explode("rnrn", $return, 2);

echo $header;

file_put_contents("/tmp/logo.png", $image);
Saturday, May 29, 2021
 
jab
answered 5 Months ago
jab
94

2 hours after...

<?
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

    $image_file = "captcha.jpg";
    //$cookie = "gcookie";

    $ch = curl_init("http://ipv4.google.com/sorry/image?id=2125815723022350864&hl=en");  
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);      
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    $data = curl_exec($ch);
    //split the header and body of request
    $matches = preg_split('/^s*$/im', $data);
    $header = $matches[0];
    //extract cookie from header
    preg_match_all('/Set-Cookie: (.*?)s+/i', $header, $gCookie, PREG_PATTERN_ORDER);
    $gCookie = $gCookie[1][0];
    echo $gCookie;
//GOOGLE_ABUSE_EXEMPTION=ID=a85908efa22e6f9b:TM=1429660423:C=c:IP=x.x.x.x-:S=APGng0vbHyNi1KCn9O1bnspO8BgF4LFEhQ;

    //The body is the image, we cleanup the header/body line break and save it
    $body = $matches[1] ;
    $body = implode("n", array_slice(explode("n", $body), 1));
    file_put_contents($image_file, $body);

curl_close($ch); 

It wasn't difficult after understanding that when we set CURLOPT_HEADER, 1 the response headers come inside $data = curl_exec($ch);, then, we just need to split the header and body to find the cookie in the header and save the body (image) to a file.

Saturday, May 29, 2021
 
osondoar
answered 5 Months ago
27

Simple:

use this code:

//to get the image from the ImageView (say iv)
BitmapDrawable draw = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = draw.getBitmap();

FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();

Additionally, in order to refresh the gallery and to view the image there:

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(file));
    sendBroadcast(intent);

Also make sure that your app has the storage permission enabled:

Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!

Manifest permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Tuesday, August 24, 2021
 
Optimight
answered 2 Months ago
65

If you only want to convert the image data to JPEG then all you need is imagecreatefromstring and imagejpeg. Basically:

imagejpeg(imagecreatefromstring($gif_bindata), "temp.jpeg");
$jpeg_bindata = file_get_contents("temp.jpeg");

It's that simple because imagecreatefromstring detects the file type automatically (the first few bytes contain enough magic bytes to make detection feasible). And obviously you might want to use a real temporary filename instead.

Monday, August 30, 2021
 
tuckerjt07
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :