Page 1 of 2

Re-creating thumbnails of images in product attributes

Posted: 08 Jan 2023, 15:47
by zucha.imz
Hi,

I found another problem, or feature needing.
I have a products, and every product has a aprox. 20 attributes of color.
Every attribute color has a two images - small and full image. I select a image, and phoca cart create a thumbnail from full image. But a change a thumbs size and I need recreate attribute image thumbnails.

Function in product list "Recreate thumbs", recreate only main image of product, no images from attributes.
Only solutions what I found is, delete all thumbs and manualy go attribute by attribute and procduct by product and open select image dialog and select image. After that phocacart create new thumb of image. But this si pain about 100 products and every has 20 attributes with image.

Thx for help.

Re: Re-creating thumbnails of images in product attributes

Posted: 12 Jan 2023, 22:23
by Jan
Hi, yes, unfortunately, there is no function to recreate attribute thumbnails. Added into feature request list.

https://github.com/PhocaCz/PhocaCart/issues/143

Jan

Re: Re-creating thumbnails of images in product attributes

Posted: 02 Feb 2023, 16:09
by zucha.imz
Hi, any chance to add this feature in few days or weeks ?

Thanks

Re: Re-creating thumbnails of images in product attributes

Posted: 03 Feb 2023, 14:35
by zucha.imz
Or can I run recreating manualy by some php scirpt ?

Re: Re-creating thumbnails of images in product attributes

Posted: 03 Feb 2023, 15:51
by zucha.imz
OK, fast hack is create a unpublished item and add additional images to them. (These images ar images of atributes, uses in other items with no recreated thumbnail). After that simply click recreate thumbnail for this one new created item.... and thmubnails are generated.

Re: Re-creating thumbnails of images in product attributes

Posted: 03 Feb 2023, 17:48
by Jan
BTW:
"Or can I run recreating manualy by some php scirpt ?"

https://www.phoca.cz/phocacart-extensio ... ole-plugin

You can recreate images per CLI, e.g. with command:

php phocacart:categories:recreatethumbnails

Jan

Re: Re-creating thumbnails of images in product attributes

Posted: 06 Feb 2023, 10:54
by zucha.imz
Thanks, that is awesome :)

Re: Re-creating thumbnails of images in product attributes

Posted: 29 Mar 2023, 00:46
by zucha.imz
Hi Jan,

I edited a CLI and now I can generate thumbs for main image, for additional image and added generate thumbs for attribute image

code of edited function doExecute in file plugins/console/phocacart/src/Command/Product/RecreateThumbnails.php

maybe usefull for somebody, or you can add to your console plugin for everyone ...

Code: Select all

<?php

	protected function doExecute(InputInterface $input, OutputInterface $output): int
	{
		$this->loadComponent();
		$this->configureIO($input, $output);
		$db = $this->getDatabase();
		$warnings = [];
		$successCount = 0;
		$itemsCount = 0;

		$query = $db->getQuery(true)
			->select($db->quoteName('p.id'))
			->select($db->quoteName('p.image'))
			->from($db->quoteName('#__phocacart_products') . ' as ' . $db->quoteName('p'))
			->where($db->quoteName('p.image') . ' <> ' . $db->quote(''));

		if ($ids = $input->getOption('id')) {
			$ids = explode(',', $ids);
			$ids = ArrayHelper::toInteger($ids);
			if ($ids)
				$query->where($db->quoteName('p.id') . ' in (' . implode(', ', $ids) . ')');
		}

		if ($categories = $input->getOption('cat')) {
			$categories = explode(',', $categories);
			$categories = ArrayHelper::toInteger($categories);
			if ($categories) {
				$query->join('INNER', $db->quoteName('#__phocacart_product_categories') . ' as ' . $db->quoteName('c'), $db->quoteName('c.product_id') . ' = ' . $db->quoteName('p.id'));
				$query->where($db->quoteName('c.category_id') . ' in (' . implode(', ', $categories) . ')');
			}
		}

		$db->setQuery($query);
		$items = $db->loadObjectList();
		$itemsCount += count($items);
		if ($items) {
			$this->ioStyle->info('Recreating main image thumbnails');
			$this->recreateThumbnails($items, 'productimage', $warnings, $successCount);
		}

		$query = $db->getQuery(true)
			->select($db->quoteName('p.product_id') . ' as ' . $db->quoteName('id'))
			->select($db->quoteName('p.image'))
			->from($db->quoteName('#__phocacart_product_images') . ' as ' . $db->quoteName('p'));

		if ($ids)
			$query->where($db->quoteName('p.product_id') . ' in (' . implode(', ', $ids) . ')');

		if ($categories) {
			$query->join('INNER', $db->quoteName('#__phocacart_product_categories') . ' as ' . $db->quoteName('c'), $db->quoteName('c.product_id') . ' = ' . $db->quoteName('p.product_id'));
			$query->where($db->quoteName('c.category_id') . ' in (' . implode(', ', $categories) . ')');
		}

		$db->setQuery($query);
		$items = $db->loadObjectList();
		$itemsCount += count($items);
		if ($items) {
			$this->ioStyle->info('Recreating additional image thumbnails');
			$this->recreateThumbnails($items, 'productimage', $warnings, $successCount);
		}


		// Recreating thumbs for attribute images
		$query = $db->getQuery(true)
			->select($db->quoteName('p.id'))
			->select($db->quoteName('p.image'))
			->from($db->quoteName('#__phocacart_attribute_values') . ' as ' . $db->quoteName('p'));

			$query->join('INNER', $db->quoteName('#__phocacart_attributes') . ' as ' . $db->quoteName('a'), $db->quoteName('a.id') . ' = ' . $db->quoteName('p.attribute_id'));


			if ($ids) 
				$query->where($db->quoteName('a.product_id') . ' in (' . implode(', ', $ids) . ')');



			if ($categories) {
				$query->join('INNER', $db->quoteName('#__phocacart_product_categories') . ' as ' . $db->quoteName('c'), $db->quoteName('c.product_id') . ' = ' . $db->quoteName('a.product_id'));
				$query->where($db->quoteName('c.category_id') . ' in (' . implode(', ', $categories) . ')');
			}
		
		$db->setQuery($query);
		$items = $db->loadObjectList();
		$itemsCount += count($items);
		if ($items) {
			$this->ioStyle->info('Recreating atribute image thumbnails');
			$this->recreateThumbnails($items, 'productimage', $warnings, $successCount);
		}



		if ($itemsCount == 0) {
			$this->ioStyle->info('Nothing to update');
			return self::RETURN_CODE_SUCCESSFUL;
		}

		if ($warnings) {
			$this->ioStyle->warning('Following errors occured');
			$this->ioStyle->listing($warnings);
		}

		if ($successCount) {
			$this->ioStyle->success($successCount . ' thumbnails recreated successfully');

			if ($warnings)
				return self::RETURN_CODE_PARTIALLY_SUCCESSFUL;
			else
				return self::RETURN_CODE_SUCCESSFUL;
		}

		return self::RETURN_CODE_FAILED;
	}
}

Re: Re-creating thumbnails of images in product attributes

Posted: 29 Mar 2023, 00:51
by zucha.imz
But I like generate a thumbs of attribute image by button, (or by calling some scritp ) for my customer (he does not access to CLI ). Is there any fast way, to code this ? Where I can found (whitch file? ) a code for button Recreate thumbnails in administration menu in product section ? Little kick maybe help me :)

Thx

Re: Re-creating thumbnails of images in product attributes

Posted: 29 Mar 2023, 18:08
by zucha.imz
ok, who seeks will find :)

I add function to recreate thumbnails button and now recreate main image, additional image and also attribute image


in file /administrator/components/com_phocacart/models/phocacartitem.php

at line number 1169 insert this code to generate thumbs also from attribute images

Code: Select all

if (isset($v->id) && (int)$v->id > 0) {
							$query = 'SELECT p.image, p.id'.
									' FROM #__phocacart_attribute_values AS p' .
									' INNER JOIN #__phocacart_attributes as a ON a.id = p.attribute_id' .
									' WHERE a.product_id ='.(int)$v->id;
							$this->_db->setQuery($query);
							$files3 = $this->_db->loadObjectList();
							if (isset($files3) && count($files3)) {
								foreach($files3 as $k3 => $v3) {

									$original3	= PhocacartFile::existsFileOriginal($v3->image, 'productimage');
									if (!$original3) {
										// Original does not exist - cannot generate new thumbnail
										//$message = JText::_('COM_PHOCACART_FILEORIGINAL_NOT_EXISTS');
										//return false;
										continue;
									}

									// Delete old thumbnails
									$deleteThubms3 = PhocacartFileThumbnail::deleteFileThumbnail($v3->image, 1, 1, 1, 'productimage');
									if (!$deleteThubms3) {
										//$message = JText::_('COM_PHOCACART_ERROR_DELETE_THUMBNAIL');
										//return false;
										continue;
									}
									$createThubms3 = PhocacartFileThumbnail::getOrCreateThumbnail($v3->image, 0, 1,1,1,0,'productimage');
									if (!$createThubms3) {
										//$message = JText::_('COM_PHOCACART_ERROR_WHILECREATINGTHUMB');
										//return false;
										continue;
									}

								}
							}
						}