Jump to content

Check for importing Images


Harmen
 Share

Recommended Posts

Hey!

I am working on a module that imports some images. But I have some struggles with a check. I need a check because if I hit the import button twice, I have the images double. I have the following code: 

	protected function importImages()
	{
		$languages = wire("languages");
		$images = $this->get_images();
		$images->execute(array(':iso' => 'en'));
		$pictures = $images->fetchAll(PDO::FETCH_ASSOC);
		foreach ($pictures as $picture) {
			$id_product = $picture['id_product'];
			$product_page = $this->pages->get("productid=$id_product");
				// ensure output formatting is off
				$product_page->of(false);
				$str_prlink = $picture['product_name']; //make sure end of string ends with alphanumeric
				$image_name = preg_replace('/[^a-z0-9]+\Z/i', '', $str_prlink);
				//echo $picture['id_product'] . '-' . $image_name . '-' . $picture['ean13'] . '.html';
				$image_url = $picture['file_id'] . "/" . $image_name . '.jpg';
				//echo "<br />";
				$image_path = "http://www.r-go-tools.com/" . $image_url;
				//echo "file id:". $file;
				try {
					$product_page->images_product->add($image_path);
				} catch (Exception $e) {
				}
				//	$product_page->of(false);
				$product_page->save();
			}
		}

I want to delete the images that are currently in that field, and then import the new images.

How can I fix this?

Link to comment
Share on other sites

Doesn't really work. If I put your solution after the declaration of $product_page. I get at the end only 1 of all the images per product. So if there are 4, I get one. If I move it all up to the start of the function, then the PDO statement doesn't work anymore for some reason. I think I need something different. Will try your solution more extensive tomorrow. But in my first trials it doesn't work. 

Link to comment
Share on other sites

Yeah, it won't work if you have it in the loop because it will delete the image you added in the last loop. 

If you simply want to prevent doubling of existing images by filename, you can do something like this whereby you check to see if the image already exists.

if(!$product_page->images_product->get("name={$image_name}.jpg")) $product_page->images_product->add($image_path);

 

  • Like 2
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...