public static function updateImages($id, $oldCode, $imagesFromFront) { $imagesFromFront = $imagesFromFront == null ? [] : $imagesFromFront; if (is_array($imagesFromFront)) { $dbPref = new DBPreferencesType(); $catalogPath = $dbPref->getPreference(Constants::CATALOG_PATH)[DB::TABLE_PREFERENCES__VALUE]; $mediumImageWatermarkName = $dbPref->getPreference(Constants::WATERMARK_MEDIUM_PATH)[DB::TABLE_PREFERENCES__VALUE]; $largeImageWatermarkName = $dbPref->getPreference(Constants::WATERMARK_LARGE_PATH)[DB::TABLE_PREFERENCES__VALUE]; $goodsType = new DBGoodsType(); $code = $goodsType->getCode($id); $newCode = null; if (!is_null($oldCode) && $code != $oldCode) { $newCode = $code; $code = $oldCode; } $imagesFromFileSystem = FileUtils::getFilesByPrefixByDescription(FileUtils::buildPath($catalogPath, $code), Constants::SMALL_IMAGE, "jpg"); $imagesToDelete = array_merge($imagesFromFileSystem); uasort($imagesFromFront, function ($o1, $o2) { return $o1['index'] < $o2['index'] ? -1 : 1; }); $imagesToProcessing = self::prepareImagesToProcessing($imagesFromFront, $imagesFromFileSystem, $imagesToDelete); self::removeImagesFilesBySamples(FileUtils::buildPath($catalogPath, $code), $imagesToDelete); //two steps of recreating files //step 1: rename old files to temp names (only files witch should be renamed) for ($imageIndex = 0; $imageIndex < count($imagesToProcessing); $imageIndex++) { $imageData = $imagesToProcessing[$imageIndex]; if ($imageData['new'] == 'false') { $imageNumber = FileUtils::getCatalogImageNumber($imageData['oldImage']); if ($imageNumber != null) { $smallImagePath = FileUtils::buildPath($catalogPath, $code, Constants::SMALL_IMAGE . $imageNumber . '.jpg'); $smallImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::SMALL_IMAGE . $imageData['tempName']); rename($smallImagePath, $smallImageTmpPath); $mediumImagePath = FileUtils::buildPath($catalogPath, $code, Constants::MEDIUM_IMAGE . $imageNumber . '.jpg'); $mediumImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::MEDIUM_IMAGE . $imageData['tempName']); rename($mediumImagePath, $mediumImageTmpPath); $largeImagePath = FileUtils::buildPath($catalogPath, $code, Constants::LARGE_IMAGE . $imageNumber . '.jpg'); $largeImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::LARGE_IMAGE . $imageData['tempName']); rename($largeImagePath, $largeImageTmpPath); } } } //step 2: rename to real names (old files and new) for ($imageIndex = 0; $imageIndex < count($imagesToProcessing); $imageIndex++) { $imageData = $imagesToProcessing[$imageIndex]; if ($imageData['new'] == 'false') { //if image ISN'T NEW $smallImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::SMALL_IMAGE . $imageData['tempName']); $smallImageNewPath = FileUtils::buildPath($catalogPath, $code, Constants::SMALL_IMAGE . $imageData['newName'] . '.jpg'); rename($smallImageTmpPath, $smallImageNewPath); $mediumImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::MEDIUM_IMAGE . $imageData['tempName']); $mediumImageNewPath = FileUtils::buildPath($catalogPath, $code, Constants::MEDIUM_IMAGE . $imageData['newName'] . '.jpg'); rename($mediumImageTmpPath, $mediumImageNewPath); $largeImageTmpPath = FileUtils::buildPath($catalogPath, $code, Constants::LARGE_IMAGE . $imageData['tempName']); $largeImageNewPath = FileUtils::buildPath($catalogPath, $code, Constants::LARGE_IMAGE . $imageData['newName'] . '.jpg'); rename($largeImageTmpPath, $largeImageNewPath); } else { //if image IS NEW $data = str_replace('data:image/jpeg;base64,', '', $imageData['oldImage']); $data = str_replace(' ', '+', $data); $smallImageName = FileUtils::buildPath($catalogPath, $code, Constants::SMALL_IMAGE . $imageData['newName'] . '.jpg'); if (file_put_contents($smallImageName, base64_decode($data)) == true) { $imageEditorS = new ImageEditor($smallImageName); $imageEditorS->resizeImage(Constants::SMALL_IMAGE_WIDTH, Constants::SMALL_IMAGE_HEIGHT); $imageEditorS->saveImage($smallImageName, 100); } $mediumImageName = FileUtils::buildPath($catalogPath, $code, Constants::MEDIUM_IMAGE . $imageData['newName'] . '.jpg'); if (file_put_contents($mediumImageName, base64_decode($data)) == true) { $imageEditorS = new ImageEditor($mediumImageName); $imageEditorS->resizeImage(Constants::MEDIUM_IMAGE_WIDTH, Constants::MEDIUM_IMAGE_HEIGHT); $imageEditorS->applyWatermark($mediumImageWatermarkName); $imageEditorS->saveImage($mediumImageName, 100); } $largeImageName = FileUtils::buildPath($catalogPath, $code, Constants::LARGE_IMAGE . $imageData['newName'] . '.jpg'); if (file_put_contents($largeImageName, base64_decode($data)) == true) { $imageEditorS = new ImageEditor($largeImageName); $imageEditorS->resizeImage(Constants::LARGE_IMAGE_WIDTH, Constants::LARGE_IMAGE_HEIGHT); $imageEditorS->applyWatermark($largeImageWatermarkName); $imageEditorS->saveImage($largeImageName, 100); } } } //if good KEY_ITEM was CHANGED if (!is_null($newCode)) { rename(FileUtils::buildPath($catalogPath, $code), FileUtils::buildPath($catalogPath, $newCode)); } } }