Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to split images in standard product image folder #21

Open
vstuber opened this issue Mar 21, 2018 · 0 comments
Open

Add functionality to split images in standard product image folder #21

vstuber opened this issue Mar 21, 2018 · 0 comments
Assignees
Labels
enhancement New feature or request PRIO Take care of this asap

Comments

@vstuber
Copy link
Contributor

vstuber commented Mar 21, 2018

If the standard product image folder contains too many images, image auto-detection can take a long time. The situation gets a lot better when the images are divided into multiple folders. To guarantee an even distribution, we can't simply create folders that represent the first character(s) of the image names because since product images start with product codes to allow auto-detection it is very likely that the first character(s) are the same for many images. Therefore, it is better to create a hash from the product code and use the hash's first character(s).

A function to take all images that are located directly in the standard product image folder and distribute them in three levels of 16 subfolders each could look like this (the example shows a quick draft as an API resource):

	/**
	 * Scope: BE
	 *
	 * Allowed user types: beUser
	 */
	protected function apiResource_heTestDivideImages()
	{
		$this->obj_apiReceiver->requireScope(['BE']);
		$this->obj_apiReceiver->requireUser(['beUser']);

//		$this->obj_apiReceiver->fail();
//		$this->obj_apiReceiver->set_data('resource currently deactivated');
//		return;

		$int_numImagesToProcess = 40000;

		$str_pathToStandardProductImageFolder = \LeadingSystems\Helpers\ls_getFilePathFromVariableSources($GLOBALS['TL_CONFIG']['ls_shop_standardProductImageFolder']);

		if (!file_exists(TL_ROOT . '/' . $str_pathToStandardProductImageFolder)) {
			$this->obj_apiReceiver->fail();
			$this->obj_apiReceiver->set_data('the standard folder for product images possibly doesn\'t exist.');
			return;
		}

		$arr_tmpImageFiles = scandir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder);

		$int_numImagesProcessed = 0;
		$int_numFoldersCreated = 0;

		if (is_array($arr_tmpImageFiles)) {
			foreach ($arr_tmpImageFiles as $str_imageFile) {
				if (
					$str_imageFile == '.'
					|| $str_imageFile == '..'
					|| is_dir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_imageFile)
				) {
					continue;
				}


				// Determine the pure filename without suffix
				$arr_tmpFilenameExploded = explode('.', $str_imageFile);
				$str_tmpFilenameSuffix = '.' . $arr_tmpFilenameExploded[count($arr_tmpFilenameExploded) - 1];
				$str_filenameWithoutSuffix = basename($str_imageFile, $str_tmpFilenameSuffix);

				// Determine the part of the filename that should match the product code
				$arr_tmpFilenameParts = explode($GLOBALS['TL_CONFIG']['ls_shop_standardProductImageDelimiter'], $str_filenameWithoutSuffix);

				$str_partToMatchProductCode = $str_filenameWithoutSuffix;
				if (is_array($arr_tmpFilenameParts) && count($arr_tmpFilenameParts) > 1) {
					$str_partToMatchProductCode = $arr_tmpFilenameParts[0];
				}

				$str_folderHash = md5($str_partToMatchProductCode);
				$str_folderNameLevel1 = substr($str_folderHash, 0, 1);
				$str_folderNameLevel2 = substr($str_folderHash, 0, 2);
				$str_folderNameLevel3 = substr($str_folderHash, 0, 3);

				if (!is_dir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1)) {
					mkdir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1);
					$int_numFoldersCreated++;
				}

				if (!is_dir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1 . '/' . $str_folderNameLevel2)) {
					mkdir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1 . '/' . $str_folderNameLevel2);
					$int_numFoldersCreated++;
				}

				if (!is_dir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1 . '/' . $str_folderNameLevel2 . '/' . $str_folderNameLevel3)) {
					mkdir(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1 . '/' . $str_folderNameLevel2 . '/' . $str_folderNameLevel3);
					$int_numFoldersCreated++;
				}

				rename(TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_imageFile, TL_ROOT . '/' . $str_pathToStandardProductImageFolder . '/' . $str_folderNameLevel1 . '/' . $str_folderNameLevel2 . '/' . $str_folderNameLevel3 . '/' . $str_imageFile);

				$int_numImagesProcessed++;
				if ($int_numImagesProcessed >= $int_numImagesToProcess) {
					break;
				}
			}
		}

		$this->obj_apiReceiver->success();
		$this->obj_apiReceiver->set_data('processed ' . $int_numImagesProcessed . ' images and created ' . $int_numFoldersCreated . ' folders');
	}

In the function ls_shop_generalHelper::getImagesFromStandardFolder we only have to extend the standard product image path using the hashed product code like this:

		$str_pathToStandardProductImageFolder = ls_getFilePathFromVariableSources($GLOBALS['TL_CONFIG']['ls_shop_standardProductImageFolder']);

		$str_subfolderHash = md5($str_productOrVariantCode);

		$str_subfolderNameLevel1 = substr($str_subfolderHash, 0, 1);
		$str_subfolderNameLevel2 = substr($str_subfolderHash, 0, 2);
		$str_subfolderNameLevel3 = substr($str_subfolderHash, 0, 3);


		$str_pathToStandardProductImageFolder = $str_pathToStandardProductImageFolder . '/' . $str_subfolderNameLevel1 . '/' . $str_subfolderNameLevel2 . '/' . $str_subfolderNameLevel3;
@vstuber vstuber added enhancement New feature or request PRIO Take care of this asap labels Mar 21, 2018
@vstuber vstuber self-assigned this Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request PRIO Take care of this asap
Projects
None yet
Development

No branches or pull requests

1 participant