Robin S Posted December 8, 2017 Share Posted December 8, 2017 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. 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: a URL: "https://domain.com/image.jpg" an array of URLs: ["https://domain.com/image1.jpg", "https://domain.com/image2.jpg"] 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/AddImageUrlshttps://modules.processwire.com/modules/add-image-urls/ 21 1 Link to comment Share on other sites More sharing options...
adrianromega Posted December 10, 2017 Share Posted December 10, 2017 Really useful module I consider to be this one, I have done something similar on one website but was not as elegant as this one. Thank you for your work. 2 Link to comment Share on other sites More sharing options...
hollyvalero Posted December 15, 2017 Share Posted December 15, 2017 Oh, that is Nifty!! 2 Link to comment Share on other sites More sharing options...
bernhard Posted December 15, 2017 Share Posted December 15, 2017 nice do you think you could extend this module to also store images pasted from the clipboard? PS: i think it would be great if the images were added via AJAX right after pasting (either urls or clipboard). what do you think @Robin S ? 1 Link to comment Share on other sites More sharing options...
Robin S Posted December 15, 2017 Author Share Posted December 15, 2017 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... 3 Link to comment Share on other sites More sharing options...
hezmann Posted February 3, 2018 Share Posted February 3, 2018 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 More sharing options...
Robin S Posted February 4, 2018 Author Share Posted February 4, 2018 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)? That will help me get to the bottom of this. Thanks. Link to comment Share on other sites More sharing options...
hezmann Posted February 5, 2018 Share Posted February 5, 2018 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: Source Code 1 Link to comment Share on other sites More sharing options...
Robin S Posted February 5, 2018 Author Share Posted February 5, 2018 @hezmann, please uninstall the module and then install the attached version. Please let me know if it resolves the issue or not. AddImageUrls.zip Link to comment Share on other sites More sharing options...
hezmann Posted February 5, 2018 Share Posted February 5, 2018 Looks like the same error. Link to comment Share on other sites More sharing options...
Robin S Posted February 5, 2018 Author Share Posted February 5, 2018 @hezmann, if you can give me access to your dev site (send me a message) I would be happy to take a look. Otherwise I'm out of ideas sorry. 1 Link to comment Share on other sites More sharing options...
hezmann Posted February 5, 2018 Share Posted February 5, 2018 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: I'll send you a message with login details to the new site. Thanks! 2 Link to comment Share on other sites More sharing options...
Robin S Posted February 5, 2018 Author Share Posted February 5, 2018 v0.1.2 released - adds compatibility with PW < 3.0.66 4 Link to comment Share on other sites More sharing options...
gmclelland Posted January 1, 2020 Share Posted January 1, 2020 Hey @Robin S, I thought this module was handy so I modified the AddImageUrls module and adapted it to work for file fields. I attached the new "AddFileUrls" module. Feel free to upload it to your github or publish these to the module directory. It seems to work well in my testing. Hope that helps AddFileUrls.zip 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 9, 2020 Author Share Posted January 9, 2020 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. 3 Link to comment Share on other sites More sharing options...
gmclelland Posted January 9, 2020 Share Posted January 9, 2020 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 More sharing options...
Robin S Posted January 11, 2020 Author Share Posted January 11, 2020 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. 3 1 Link to comment Share on other sites More sharing options...
gmclelland Posted January 14, 2020 Share Posted January 14, 2020 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? 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 14, 2020 Author Share Posted January 14, 2020 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 3 Link to comment Share on other sites More sharing options...
gmclelland Posted January 15, 2020 Share Posted January 15, 2020 Thanks Robin, just tested and it does work. Man, you're fast! 1 Link to comment Share on other sites More sharing options...
monollonom Posted November 18, 2021 Share Posted November 18, 2021 Hi @Robin S, I made a pull request to add a new method `addFromUrl` to Pagefiles. In a project I'm working on I needed this function from the API. Coming across your module I extracted the bit I was interested in but then I thought it might be a good addition to your module. Hope it helps! 1 Link to comment Share on other sites More sharing options...
Robin S Posted November 19, 2021 Author Share Posted November 19, 2021 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: a URL: "https://domain.com/image.jpg" an array of URLs: ["https://domain.com/image1.jpg", "https://domain.com/image2.jpg"] 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). 1 Link to comment Share on other sites More sharing options...
bernhard Posted November 20, 2021 Share Posted November 20, 2021 Wouldn't it be better to do the $page->of(...) inside the addFromUrl method? Link to comment Share on other sites More sharing options...
monollonom Posted November 20, 2021 Share Posted November 20, 2021 (edited) 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 November 20, 2021 by monollonom Link to comment Share on other sites More sharing options...
bernhard Posted November 21, 2021 Share Posted November 21, 2021 ok then $page->getUnformatted('file_field')->addFromUrl(...) would also work ? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now