Jump to content

Add Image URLs


Robin S
 Share

Recommended Posts

Add Image URLs

A module for ProcessWire CMS/CMF. Allows images/files to be added to Image/File fields by pasting URLs or using the API.

screencast

Installation

Install the Add Image URLs module.

Configuration

You can add MIME type > file extension mappings in the module config. These mappings are used when validating URLs to files that do not have file extensions.

Usage

A "Paste URLs" button will be added to all Image and File fields. Use the button to show a textarea where URLs may be pasted, one per line. Images/files are added when the page is saved.

A Pagefiles::addFromUrl method is also added to the API to achieve the same result. The argument of this method is expected to be either:

Example:

// Get unformatted value of File/Image field to be sure that it's an instance of Pagefiles
$page->getUnformatted('file_field')->addFromUrl("https://domain.com/path-to-file.ext");
// No need to call $page->save() as it's already done in the method
 

Should you have an issue using the method, please have a look at the "errors" log to check if something was wrong with your URL(s).

WebP conversion

The core InputfieldImage does not support images in WebP format. But if you have the WebP To Jpg module installed (v0.2.0 or newer) then any WebP images you add via Add Image URLs will be automatically converted to JPG format.

 

https://github.com/Toutouwai/AddImageUrls
https://modules.processwire.com/modules/add-image-urls/

  • Like 21
Link to comment
Share on other sites

5 hours ago, bernhard said:

do you think you could extend this module to also store images pasted from the clipboard?

I agree it would be handy, but that would be a completely different thing to what this module is doing. This module is essentially just using Pageimages::add() on the submitted URLs, and that method has no support for clipboard data. I don't think I want to disappear into the browser clipboard rabbit-hole with this module. :-)

5 hours ago, bernhard said:

i think it would be great if the images were added via AJAX right after pasting (either urls or clipboard).

I'll have a think about it. It would make the module significantly more complex and I'm not sure how much time I want to put into it. Maybe if I get bored sometime...

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

I'm getting an error on this...

Error: Uncaught TypeError: Argument 1 passed to ProcessWire\Pageimage::__construct() must be an instance of ProcessWire\Pagefiles, null given, called in /home/walking/public_html/site/modules/AddImageUrls/AddImageUrls.module on line 152 and defined in /home/walking/public_html/wire/core/Pageimage.php:109

Link to comment
Share on other sites

On 03/02/2018 at 1:30 PM, hezmann said:

I'm getting an error on this...

Can you give a more detailed report please?

  • What are the image URLs you are trying to add when you see the error notice?
  • Is the issue reproducible? i.e. if you try again with the same URLs do you see the same notice?
  • If you download the images to your computer and then upload as per a normal addition to an images field, do they work then?
  • Anything special about your images field - e.g. inside a Repeater or PageTable?
  • The error seems to be related to the field name. Could you please post a screenshot of your browser dev tools showing the source code of the textarea input that this module adds to the images field (like the one below)?

2018-02-05_101903.thumb.png.20073e94b74aaec6545dfec1222a2795.png

That will help me get to the bottom of this. Thanks.

 

Link to comment
Share on other sites

I've tried several URL's from various places - all get the same error

I get it with every URL so yes, reproducable.  I have not been able to save any images this way.

Saving the image and then uploading works fine.

Not inside a repeater or anything special, just a normal Images field

Full Error: image.thumb.png.fad8a91ee02bc55c323b1e2a18e494bc.png

 

Source Code

image.png.e5ff46b25d67dac405764089cc3b7c1c.png

  • Like 1
Link to comment
Share on other sites

Hey @Robin S - I thought since I've been mucking about with a lot of modules and code I would do a clean install and see what happened.  I did a 100% clean install on a site that has literally never had anything on it (I owned the domain but have never used it).  Only added one field for images with one template.  Created a page and got this error:  image.thumb.png.b057755514b928016590e05782f264ac.png

I'll send you a message with login details to the new site.  Thanks!

  • Like 2
Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...
On 1/1/2020 at 5:49 PM, gmclelland said:

I modified the AddImageUrls module and adapted it to work for file fields.

v0.2.0 released, which adds support for File fields so that file URLs can be added the same as with Image fields. Thanks to @gmclelland for the idea.

I've also submitted the module to the directory now seeing as it seems to be working well without any significant issues reported.

 

  • Like 3
Link to comment
Share on other sites

Thanks for adding this to modules directory and combining both module's functionality. ?

Not sure if this is possible, but sometimes you have sites that have file urls or image urls with no extensions.  Example: mywebsite.com/this-is-a-pdf-link or mywebsite.com/this-is-an-image

Urls like that won't work currently with this module.  I'm not sure if there is a way to add a check if the url doesn't have an extension, then check the mime type against the allowed extensions?

Maybe somehow comparing the two by using $config->fileContentTypes and $ctype = mime_content_type($file->filename); ?

Link to comment
Share on other sites

On 1/10/2020 at 7:34 AM, gmclelland said:

Not sure if this is possible, but sometimes you have sites that have file urls or image urls with no extensions. 

I've added support for these sorts of remote files in v0.2.1.

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hi @Robin S, just wanted to let you know that I tried out the latest version, but it didn't seem to work with the file/image files with no extension.

Here are some examples that didn't work for me:

Ex. pdf file with no extension:

https://www.charleston-sc.gov/DocumentCenter/View/18787/Certificate-of-Construction-Completion-RES-Checklist?bidId=

Ex. image file with no extension:

https://www.charleston-sc.gov/ImageRepository/Document?documentID=23023

After saving the page with those urls, it shows the following error message:

AddImageUrls: The remote file https://www.charleston-sc.gov/DocumentCenter/View/18787/Certificate-of-Construction-Completion-RES-Checklist?bidId= has no file extension and its MIME type does not correspond with a valid file extension for field "page_files".

The file field is configured to allow pdf files and the image field is configured to allow png, jpg, jpeg, gif.

Do these urls work for you?

  • Like 1
Link to comment
Share on other sites

6 hours ago, gmclelland said:

just wanted to let you know that I tried out the latest version, but it didn't seem to work with the file/image files with no extension.

This was due to $config->fileContentTypes not having up-to-date MIME types. I created an issue here and decided it would be better to make the MIME type > file extension mapping configurable in v0.2.2 rather than using $config->fileContentTypes. Please update the module and the problem should be fixed.

P.S. This page is a useful resource if anyone wants to set additional MIME type mappings: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

Thanks to @monollonom in v0.2.5 you can now use the module to add files/images to fields using the API. From the updated readme:

A addFromUrl method is also added to the API to achieve the same result. The argument of this method is expected to be either:

Example:

$of = $page->of();
$page->of(false);
$page->file_field->addFromUrl("https://domain.com/path-to-file.ext");
// No need to call $page->save() as it's already done in the method
$page->of($of);

Should you have an issue using the method, please have a look at the "errors" log to check if something was wrong with your URL(s).

  • Like 1
Link to comment
Share on other sites

It’s already the case.

The reason I set off the output formatting in the example is in the case your image field is set to accepting only one file and thus returning the image or null if empty. Setting output formatting makes sure we get the Pageimages object to call the method from.

(but it could be mentioned then I guess)

Edited by monollonom
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...