private:koding:hostcms:modules:shop:useful:resize_images:resizer.php
<?php
 
require __DIR__ . '/bootstrap.php';
 
defined('CHMOD') || define('CHMOD', 0755);
defined('CHMOD_FILE') || define('CHMOD_FILE', 0644);
 
abstract class ImageResizer
{
	protected $_backupDir;
 
	protected $_executed = 0;
 
	protected $_limit = 20;
 
	protected $_offset = 0;
 
	protected $_properties = array();
 
	public function __construct()
	{
		$this->_backupDir = CMS_FOLDER . TMP_DIR . 'image_resizer/';
 
		Core_File::mkdir($this->_backupDir);
	}
 
	public function getExecuted()
	{
		return $this->_executed;
	}
 
	public function limit($limit)
	{
		$this->_limit = $limit;
 
		return $this;
	}
 
	public function offset($offset)
	{
		$this->_offset = $offset;
 
		return $this;
	}
 
	public function properties($properties)
	{
		$this->_properties = $properties;
 
		return $this;
	}
 
	abstract public function execute();
 
	protected function _resizeEntity($oParentEntity, $oCoreEntity)
	{
		$this->_executed++;
 
		// Бэкапим файлы
		$largeFile = $this->_backupFile($oCoreEntity, 'large', $oCoreEntity->getLargeFilePath());
		$smallFile = $this->_backupFile($oCoreEntity, 'small', $oCoreEntity->getSmallFilePath());
 
		if (!$largeFile && !$smallFile)
		{
			$this->_log('skip ' . $oCoreEntity->getModelName() . '#' . $oCoreEntity->id);
			return;
		}
 
		$this->_log('resize ' . $oCoreEntity->getModelName() . '#' . $oCoreEntity->id);
 
		// Большое изображение
		$largeMaxWidth = $oParentEntity->image_large_max_width;
		$largeMaxHeight = $oParentEntity->image_large_max_height;
 
		if ($largeFile)
		{
			$largeImage = '' . $oCoreEntity->getModelName() . 's_catalog_image_' . $oCoreEntity->id . '.' . $this->_getExtension($largeFile);
 
			if ($oCoreEntity->getModelName() == 'property_value_file')
			{
				$oCoreEntity->file = $largeImage;
			}
			else
			{
				$oCoreEntity->image_large = $largeImage;
			}
 
			Core_Image::resizeImage($largeFile, $largeMaxWidth, $largeMaxHeight, $oCoreEntity->getLargeFilePath());
 
			$this->_log('resize large file ' . $oCoreEntity->getLargeFilePath() . ' from ' . $largeFile);
		}
 
		// Малое изображение
		$smallMaxWidth = $oParentEntity->image_small_max_width;
		$smallMaxHeight = $oParentEntity->image_small_max_height;
 
		if ($smallFile)
		{
			$smallImage = 'small_' . $oCoreEntity->getModelName() . 's_catalog_image' . $oCoreEntity->id . '.' . $this->_getExtension($smallFile);
 
			if ($oCoreEntity->getModelName() == 'property_value_file')
			{
				$oCoreEntity->file_small = $smallImage;
			}
			else
			{
				$oCoreEntity->image_small = $smallImage;
			}
 
			Core_Image::resizeImage($smallFile, $smallMaxWidth, $smallMaxHeight, $oCoreEntity->getSmallFilePath());
 
			$this->_log('resize small file ' . $oCoreEntity->getSmallFilePath() . ' from ' . $smallFile);
		}
		elseif ($largeFile)
		{
			$smallImage = 'small_' . $oCoreEntity->getModelName() . 's_catalog_image' . $oCoreEntity->id . '.' . $this->_getExtension($largeFile);
 
			if ($oCoreEntity->getModelName() == 'property_value_file')
			{
				$oCoreEntity->file_small = $smallImage;
			}
			else
			{
				$oCoreEntity->image_small = $smallImage;
			}
 
			Core_Image::resizeImage($largeFile, $smallMaxWidth, $smallMaxHeight, $oCoreEntity->getSmallFilePath());
 
			$this->_log('resize small file (from large large) ' . $oCoreEntity->getSmallFilePath() . ' from ' . $largeFile);
		}
 
		$oCoreEntity->save();
	}
 
	protected function _backupFile($oCoreEntity, $tag, $file)
	{
		if (!is_file($file) || !file_exists($file))
		{
			return NULL;
		}
 
		$filename = $oCoreEntity->getModelName() . '_' . $tag . '_' . $oCoreEntity->id;
		$ext = $this->_getExtension($file);
 
		$backupFile = $this->_backupDir . $filename . '.' . $ext;
 
		if (!file_exists($backupFile))
		{
			Core_File::copy($file, $backupFile);
		}
 
		return $backupFile;
	}
 
	protected function _getExtension($file)
	{
		return pathinfo($file, PATHINFO_EXTENSION);
	}
 
	protected function _log($log)
	{
		$line = Core_Date::timestamp2sql(time()) . ' -- ' . $log . "\n";
 
		print $line;
		file_put_contents(CMS_FOLDER . 'hostcmsfiles/logs/image_resizer.log', $line, FILE_APPEND);
	}
}
 
class ShopItemImageResizer extends ImageResizer
{
	protected $_Shop;
 
	public function __construct(Shop_Model $oShop)
	{
		parent::__construct();
 
		$this->_Shop = $oShop;
		$this->_ShopItems = $oShop->Shop_Items;
	}
 
	public function shopItems()
	{
		return $this->_ShopItems;
	}
 
	public function execute()
	{
		$this->_log('execute resizing shop #' . $this->_Shop->id);
 
		$this->_ShopItems->queryBuilder()
			->offset($this->_offset)
			->limit($this->_limit);
 
		$aoShopItems = $this->_ShopItems->findAll();
 
		foreach ($aoShopItems as $oShopItem)
		{
			$this->_resizeEntity($this->_Shop, $oShopItem);
 
			// Доп. свойства
			if (count($this->_properties))
			{
				$aoImagePropertyValues = $oShopItem->getPropertyValues(TRUE, $this->_properties);
 
				foreach ($aoImagePropertyValues as $oImagePropertyValue)
				{
					$this->_resizeEntity($this->_Shop, $oImagePropertyValue);
				}
			}
 
			$this->_log('');
		}
	}
}
 
 
class InformationsystemItemImageResizer extends ImageResizer
{
	protected $_Informationsystem;
 
	public function __construct(Informationsystem_Model $oInformationsystem)
	{
		parent::__construct();
 
		$this->_Informationsystem = $oInformationsystem;
		$this->_InformationsystemItems = $oInformationsystem->Informationsystem_Items;
	}
 
	public function informationsystemItems()
	{
		return $this->_InformationsystemItems;
	}
 
	public function execute()
	{
		$this->_log('execute resizing informationsystem #' . $this->_Informationsystem->id);
 
		$this->_InformationsystemItems->queryBuilder()
			->offset($this->_offset)
			->limit($this->_limit);
 
		$aoInformationsystemItems = $this->_InformationsystemItems->findAll();
 
		foreach ($aoInformationsystemItems as $oInformationsystemItem)
		{
			$this->_resizeEntity($this->_Informationsystem, $oInformationsystemItem);
 
			// Доп. свойства не учитываются
 
			$this->_log('');
		}
	}
}
 
class ImageResizeRunner
{
	protected $_imageResizer;
 
	public function __construct(ImageResizer $oImageResizer)
	{
		$this->_imageResizer = $oImageResizer;
	}
 
	public function run()
	{
		$limit = 1;
		$offset = (int) Core_Array::getGet('offset');
 
		$this->_imageResizer
			->offset($offset)
			->limit($limit)
			->execute();
 
		if ($this->_imageResizer->getExecuted())
		{
			?>
				<script>
					setTimeout(function () {
						location.assign('?offset=<?=$offset + $limit?>');
					}, 1000);
				</script>
			<?php
		}
		else
		{
			?>
				execution stop
			<?php
		}
	}
}
 
// Интернет-магазин
$oImageResizer = new ShopItemImageResizer(Core_Entity::factory('Shop', xxx));
 
// Для тестирования можно ставить добавлять условия
// $oImageResizer->shopItems()
// 	->queryBuilder()
// 		->where('shop_items.shop_group_id', '=', 860);
 
// Доп. свойства
$oImageResizer->properties(array(4460, 4443, 4444));
 
$oImageResizeRunner = new ImageResizeRunner($oImageResizer);
$oImageResizeRunner->run();
 
// // Информационная система #62
// $oImageResizer = new InformationsystemItemImageResizer(Core_Entity::factory('Informationsystem', 62));
// $oImageResizeRunner = new ImageResizeRunner($oImageResizer);
// $oImageResizeRunner->run();
private/koding/hostcms/modules/shop/useful/resize_images/resizer.php.txt · Last modified: 29.09.17 в 17:18 by maximzasorin_gmail.com