Jump to content
Xonox

[Solved] API image upload I need to replace instead of adding

Recommended Posts

Hi,

 

I'm trying to upload images from a folder into a page. I need to replace the images instead of adding. The image field already has the replace existing images turned on, but it doesn't seem to be enough.

 

My code:

foreach($files as $file) {

	if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') {

		// Get SKU
		$file_sku = substr($file, 0, 9);

		// Check for book
		$book = $pages->get('sku=' . $file_sku);
		if(!$book->id) {

			// Book doesn't exist

		}
		else {

			// Upload image into book !!! THIS CODE ADDS FILE INSTEAD OF REPLACING. HOW CAN I REPLACE?
			$book->book_images->add($upload_directory . '/' . $file);

			// Delete file
			unlink($upload_directory . '/' . $file);

		}

		// Save book
		if($book->id) $book->save();

	}

}

 

What am I missing?

Share this post


Link to post
Share on other sites

Just put in a $book->book_images->deleteAll() before the "add"

I don't think that replace setting is honored by the API.

Share this post


Link to post
Share on other sites
5 hours ago, adrian said:

I don't think that replace setting is honored by the API.

I don't think any of the image field settings are honoured by $pageimages->add(). Not "valid file extensions", not "maximum files allowed", not min/max width/height, etc. It's a shame as it means you have to do a lot of manual validation if using add().

  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, Robin S said:

I don't think any of the image field settings are honoured by $pageimages->add(). Not "valid file extensions", not "maximum files allowed", not min/max width/height, etc. It's a shame as it means you have to do a lot of manual validation if using add().

Yeah, pretty sure you're right. 

Here's an old discussion about it in case you want to read some of Ryan's thoughts on the matter:
https://github.com/ryancramerdesign/ProcessWire/issues/1122

  • Like 1

Share this post


Link to post
Share on other sites
24 minutes ago, Robin S said:

It's a shame as it means you have to do a lot of manual validation if using add().

I had to handle this kind of validation in my AddImageUrls module, so the code there might be useful for anyone else needing to do the same: https://github.com/Toutouwai/AddImageUrls/blob/f67ebe9729fc54d629fe939be2f9d3e9c7f68c16/AddImageUrls.module#L118-L170

  • Like 2

Share this post


Link to post
Share on other sites

Hi,

Thank you for your help.

Here's what I've got so far and working:

foreach($files as $file) {

	if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') {

		// Get SKU
		$file_sku = substr($file, 0, 9);

		// Check for book
		$book = $pages->get('sku=' . $file_sku);
		if(!$book->id) {

			// Book doesn't exist

		}
		else {

			// Check if image already exists
			foreach($book->book_images as $image){

				// Remove existing image
				if($file == $image) {
					$book->book_images->remove($image);
					$book->save();
				}

			}

			// Add new image
			$book->book_images->add($upload_directory . '/' . $file);

		}

		// Save book
		if($book->id) $book->save();

	}

}

The only problem with this code, is that the new image doesn't keep the same order. It's always added at the end. Is there any way to make it load into the same position?

Share this post


Link to post
Share on other sites
27 minutes ago, adrian said:

Note the mention of the new ->replace() method as well

@adrian thank you very much for your help, so far. However, it doesn't seems to be working:

$book->book_images->replace($book->book_images->path . $image, $upload_directory . '/' . $file);

Doesn't return any error but it doesn't replace the image! 😞

Share this post


Link to post
Share on other sites
2 hours ago, Xonox said:

Doesn't return any error but it doesn't replace the image!

Ryan's example using insertAfter works for me - just add in the missing: $page->of(false); line at the top.

Did you try that?

Share this post


Link to post
Share on other sites

@adrian, thanks for your help. The examples given weren't really helping because the file name wasn't kept. For each time de file was replace it would add "-1", "-2" etc. to the file name. I managed to make it work and I am posting the code, in case someone needs something like this.

foreach($files as $file) {

	if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') {

		// Get SKU
		$file_sku = substr($file, 0, 9);

		// Check for book
		$book = $pages->get('sku=' . $file_sku);
		if(!$book->id) {

			// Book doesn't exist

		}
		else {

			// Check if image already exists
			$replace = false;
			foreach($book->book_images as $image){

				// Replace image
				if($file == $image) {
					$book->book_images->delete($image);
					$book->save();
					$book->book_images->add($upload_directory . '/' . $file);
					$book->save();

					// Place image in the right position: after previous or as first
					if(isset($previous_image)) {
						$old_item = $book->book_images->get($previous_image);
						$new_item = $book->book_images->last();
						$book->book_images->insertAfter($new_item, $old_item);
					}
					else {
						$old_item = $book->book_images->first(); 
						$new_item = $book->book_images->last();
						$book->book_images->insertBefore($new_item, $old_item); 
					}
					$replace = true;

				}

				$previous_image = $image;

			}

			// Add image to book
			if(!$replace) {
				$book->book_images->add($upload_directory . '/' . $file);
			}

			// Save book
			$book->save();

			// Delete processed file
			unlink($upload_directory . '/' . $file);

		}

	}

}

 

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Chris Bennett
      Hi all, I am going round and round in circles and would greatly appreciate if anyone can point me in the right direction.
      I am sure I am doing something dumb, or missing something I should know, but don't. Story of my life 😉

      Playing round with a module and my basic problem is I want to upload an image and also use InputfieldMarkup and other Inputfields.
      Going back and forth between trying an api generated page defining Fieldgroup, Template, Fields, Page and the InputfieldWrapper method.

      InputfieldWrapper method works great for all the markup stuff, but I just can't wrap my head around what I need to do to save the image to the database.
      Can generate a Field for it (thanks to the api investigations) but not sure what I need to do to link the Inputfield to that. Tried a lot of stuff from various threads, of varying dates without luck.
      Undoubtedly not helped by me not knowing enough.

      Defining Fieldgroup etc through the api seems nice and clean and works great for the images but I can't wrap my head around how/if I can add/append/hook the InputfieldWrapper/InputfieldMarkup stuff I'd like to include on that template as well. Not even sure if it should be where it is on ___install with the Fieldtype stuff or later on . Not getting Tracy errors, just nothing seems to happen.
      If anyone has any ideas or can point me in the right direction, that would be great because at the moment I am stumbling round in the dark.
       
      public function ___install() { parent::___install(); $page = $this->pages->get('name='.self::PAGE_NAME); if (!$page->id) { // Create fieldgroup, template, fields and page // Create new fieldgroup $fmFieldgroup = new Fieldgroup(); $fmFieldgroup->name = MODULE_NAME.'-fieldgroup'; $fmFieldgroup->add($this->fields->get('title')); // needed title field $fmFieldgroup->save(); // Create new template using the fieldgroup $fmTemplate = new Template(); $fmTemplate->name = MODULE_NAME; $fmTemplate->fieldgroup = $fmFieldgroup; $fmTemplate->noSettings = 1; $fmTemplate->noChildren = 1; $fmTemplate->allowNewPages = 0; $fmTemplate->tabContent = MODULE_NAME; $fmTemplate->noChangeTemplate = 1; $fmTemplate->setIcon(ICON); $fmTemplate->save(); // Favicon source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFavicon'; $fmField->label = 'Favicon'; $fmField->focusMode = 'off'; $fmField->gridMode = 'grid'; $fmField->extensions = 'svg png'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Favicon Silhouette source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFaviconSilhouette'; $fmField->label = 'SVG Silhouette'; $fmField->notes = 'When creating a silhouette/mask svg version for Safari Pinned Tabs and Windows Tiles, we recommend setting your viewbox for 0 0 16 16, as this is what Apple requires. In many cases, the easiest way to do this in something like illustrator is a sacrificial rectangle with no fill, and no stroke at 16 x 16. This forces the desired viewbox and can then be discarded easily using something as simple as notepad. Easy is good, especially when you get the result you want without a lot of hassle.'; $fmField->focusMode = 'off'; $fmField->extensions = 'svg'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Create: Open Settings Tab $tabOpener = new Field(); $tabOpener->type = new FieldtypeFieldsetTabOpen(); $tabOpener->name = 'fmTab1'; $tabOpener->label = "Favicon Settings"; $tabOpener->collapsed = Inputfield::collapsedNever; $tabOpener->addTag(MODULE_NAME); $tabOpener->save(); // Create: Close Settings Tab $tabCloser = new Field(); $tabCloser->type = new FieldtypeFieldsetClose; $tabCloser->name = 'fmTab1' . FieldtypeFieldsetTabOpen::fieldsetCloseIdentifier; $tabCloser->label = "Close open tab"; $tabCloser->addTag(MODULE_NAME); $tabCloser->save(); // Create: Opens wrapper for Favicon Folder Name $filesOpener = new Field(); $filesOpener->type = new FieldtypeFieldsetOpen(); $filesOpener->name = 'fmOpenFolderName'; $filesOpener->label = 'Wrap Folder Name'; $filesOpener->class = 'inline'; $filesOpener->collapsed = Inputfield::collapsedNever; $filesOpener->addTag(MODULE_NAME); $filesOpener->save(); // Create: Close wrapper for Favicon Folder Name $filesCloser = new Field(); $filesCloser->type = new FieldtypeFieldsetClose(); $filesCloser->name = 'fmOpenFolderName' . FieldtypeFieldsetOpen::fieldsetCloseIdentifier; $filesCloser->label = "Close open fieldset"; $filesCloser->addTag(MODULE_NAME); $filesCloser->save(); // Create Favicon Folder Name $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeText"); $fmField->name = 'folderName'; $fmField->label = 'Favicon Folder:'; $fmField->description = $this->config->urls->files; $fmField->placeholder = 'Destination Folder for your generated favicons, webmanifest and browserconfig'; $fmField->columnWidth = 100; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon('folder'); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($tabOpener); $fmFieldgroup->add($filesOpener); $fmFieldgroup->add($fmField); $fmFieldgroup->add($filesCloser); $fmFieldgroup->add($tabCloser); $fmFieldgroup->save(); /////////////////////////////////////////////////////////////// // Experimental Markup Tests $wrapperFaviconMagic = new InputfieldWrapper(); $wrapperFaviconMagic->attr('id','faviconMagicWrapper'); $wrapperFaviconMagic->attr('title',$this->_('Favicon Magic')); // field show info what $field = $this->modules->get('InputfieldMarkup'); $field->name = 'use'; $field->label = __('How do I use it?'); $field->collapsed = Inputfield::collapsedNever; $field->icon('info'); $field->attr('value', 'Does this even begin to vaguely work?'); $field->columnWidth = 50; $wrapperFaviconMagic->add($field); $fmTemplate->fields->add($wrapperFaviconMagic); $fmTemplate->fields->save(); ///////////////////////////////////////////////////////////// // Create page $page = $this->wire( new Page() ); $page->template = MODULE_NAME; $page->parent = $this->wire('pages')->get('/'); $page->addStatus(Page::statusHidden); $page->title = 'Favicons'; $page->name = self::PAGE_NAME; $page->process = $this; $page->save(); } }  
    • By marcus
      wireshell 1.0.0 is out    
      See Bea's post
       


      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...