Jump to content

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


Xonox
 Share

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?

Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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?

Link to comment
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! ?

Link to comment
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
Link to comment
Share on other sites

  • Xonox changed the title to [Solved] API image upload I need to replace instead of adding

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...