Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AddImagesFromUrl.module
<?php
/**
* AddImagesFromUrl
*
* On a page with fields
* "add_images_url" text field
* "images" images field
*
* This basic example module will add the image from the url to the images field on page save.
*
* 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)
*
*
*/
class AddImagesFromUrl extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
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;
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 = '';
}
}
}
@somatonic

This comment has been minimized.

Copy link
Owner Author

commented Nov 17, 2014

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)

@horst-n

This comment has been minimized.

Copy link

commented Feb 14, 2015

I'm not sure if it depends on Browsers or Systems, but for interactive adding URLs to imagesfields I just can input URLs into the FilesUpload Inputfield of the core imagesfield. (Win7 FireFox)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.