3zzy 3zzy - 1 year ago 76
Javascript Question

Saving remote image with PhantomJS

I'm using PhantomJS 2.1.1 on Ubuntu without Node or Casper.

fs.write('images/products/image.jpg', 'http://example.com/folder/someimage.jpg', 'w');

.. although this creates 1xx byte jpeg files that aren't really images. Is there a way to download any type of (JPEG, JPG, PNG) file with PhantomJS?

Answer Source

I ended up doing this:

$stdout = shell_exec("phantomjs ./scrape-images.js '".$url);
$images = isset($stdout) ? explode(',', $stdout) : '';

.. then:

$command = 'wget '.urlencode($image).' --output-document="/path/to/image/directory/'.$filename.'" --quiet --background >/dev/null 2>&1';

Edit: The only downside is that some websites detect wget and throw a 404 (even though I pass a useragent and a referer) for not being a real user whereas Phantom would've gotten away with it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download