Jump to content
rick

add image zip from url

Recommended Posts

I know I have to be missing something simple.

I need to specify via API a url to a zip file which contains images and have them saved in a back-end images field when the admin form is submitted. I have built a process module which generates a dynamic form and processes the form once submitted. Everything functions correctly except adding a zip archive of images to a multi-image field.

  • The images field works fine if the zip file is selected on a local machine.
  • The addimageurls module does not work with zip files.
  • The wirehttp->download will save the zip file to the specified folder, but the path returned is not accepted by the images field.
// $zip can be filespec or uri
$page->of(false);
$page->images->add($zip);
$page->save();

The images field has no file limit, zip extension added, and decompress zip is enabled.

Can someone provide the correct procedure please?

Share this post


Link to post
Share on other sites

I think you have to unzip it firstly and then add images to your images field

$zip = $config->paths->cache . "archive.zip";
$dst = $config->paths->cache . "temp-zip/";
$items = $files->unzip($zip, $dst);
$page->of(false);
if(count($items)) {
	foreach($items as $item) {
		$page->images->add($item);
	}
}
$page->save();

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you @Zeka. Your solution does work.

The base images field allows for a local zip file to be added via the Choose File option. I haven't found a means to accomplish this from the API.

For example, from the API:

  1. Download the desired file.zip file to a local folder. $this->config->paths->assets . "some-folder/" . basename( $url );
  2. Pass the file from that local folder path to the images field. $page->images->Add( "/var/www/test/site/assets/some-folder/file.zip" );
  3. Save the page.

Does not work. However, if I select Choose File and navigate to that same folder, select file.zip, and click Open, the zip file contents are added to the images field. So there is a difference I haven't yet been able to determine.

 

Share this post


Link to post
Share on other sites
15 hours ago, rick said:

So there is a difference I haven't yet been able to determine.

The unzipping behaviour is part of the inputfield itself not the PageFiles/PageImages class: https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module#L943-L945 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...