Jump to content

Grab and save image from url to ProcessWire


salles
 Share

Recommended Posts

Hello!  :)

I have a cool upcoming project which its content will heavily depend on external images. Although I'd like to get rid of WordPress for this project and would love to use ProcessWire, WP has a great plugin called Grab & Save, that allows you to grab and save images from remote urls into your own WP media library.

I was wondering if ProcessWire has a similar plugin or functionality, which it would be a huge time saver for these kind of projects!  :P

Link to comment
Share on other sites

Something you have on API side which is simple really.

$p = $pages->get(1001);
$p->of(true); // if in a template code, turn off output formatting
$p->images->add("http://placehold.it/350x150.jpg");
$p->save();
$p->of(false);

Everything is possible. With a simple module.

As a basic start add a text or url field to the template near your "images" field, like a text field "add_images_url"

Now with a autoload module (like site/modules/HelloWorld.module) you add a hook to when the field has url it will store it to the "images" field (or any on the page). All you need is to enter url and save page.

<?php

/**
 * AddImagesFromUrl
 * 
 * On a page with fields
 * "add_images_url" text field
 * "images" images field
 *
 */

class AddImagesFromUrl extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'AddImagesFromUrl',
            'version' => 101,
            'summary' => 'Add images from url to images field',
            'href' => 'http://www.processwire.com',
            'singular' => true,
            'autoload' => "template=admin",
            'icon' => 'smile-o',
            );
    }

    public function init() {

        $this->addHookAfter('Pages::saveReady', $this, 'addImage');

    }

    public function addImage(HookEvent $event) {

        $page = $event->arguments("page");

        if($page->template != "basic-page") return;
        if(!$page->add_images_url) return;
        
        // now interesting part, anything is possible here
        if(strpos($page->add_images_url, ".jpg") != false) {
            $page->images->add($page->add_images_url);
            $this->message("Added image from '$page->add_images_url' to images field");
            $page->add_images_url = '';
        }

    }

}

https://gist.github.com/somatonic/49f9e0a7faa8f6e6cfa3

  • Like 4
Link to comment
Share on other sites

This would be just meant as an example! 

- Maybe this could lead to problems if you don't test for if the image is really an image before loading into the images field (local disk). Would need confirmation through if PW isn't already checking (?)

- Copyrights?

- Probably could need some check for allow_url_fopen (not sure either on this one)

(Make it your own and modify)

Link to comment
Share on other sites

@Soma - That's so awesome, thank you very much for writing this module! I can't wait to get home and try it!  ^_^

As for the copyrights, I was planning to add the images sources via a page fieldtype, manually. For example, if the image it's from Flickr, I'd manually add "flickr.com".

But come to think of it, is there a way to automatically save/pull the main url in some page fieldtype? So for example if the image url is "http://flickr.com/some-image-etc.jpg", it would automatically save/store "flickr.com" in some page fieldtype. Too tricky?  :mellow:

-Edit

Just tried it, works like a charm!

Small note though: I can't delete the image later if I want to, nothing happens when I click on the trash icon... :mellow:

Once again thank you, Soma!  ^_^  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...