Forked from michaelhoang/command-generate-image.php
Created
September 21, 2020 19:31
-
-
Save johnulist/0d0a0992c22e745a721d9c70b34a31f2 to your computer and use it in GitHub Desktop.
Revisions
-
michaelhoang revised this gist
Aug 19, 2015 . 1 changed file with 82 additions and 56 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,4 @@ <?php $timer_start = microtime(true); if (!defined('_PS_ADMIN_DIR_')) define('_PS_ADMIN_DIR_', getcwd()); @@ -16,68 +13,97 @@ class CommandRegenerateImage extends AdminImagesControllerCore { public function regenerateThumbnails($type = 'all', $deleteOldImages = true) { echo "Begin regenerate image:"; if (!$this->_regenerateThumbnails($type, $deleteOldImages)) { echo "\nHas a problem!"; foreach($this->errors as $error) { echo "\n" . $error; } } else { echo "\nFinished!"; } } protected function _regenerateNewImages($dir, $type, $productsImages = false) { if (!is_dir($dir)) return false; $generate_hight_dpi_images = (bool)Configuration::get('PS_HIGHT_DPI'); if (!$productsImages) { foreach (scandir($dir) as $image) if (preg_match('/^[0-9]*\.jpg$/', $image)) foreach ($type as $k => $imageType) { // Customizable writing dir $newDir = $dir; if ($imageType['name'] == 'thumb_scene') $newDir .= 'thumbs/'; if (!file_exists($newDir)) continue; echo "\n" . $newDir . substr($image, 0, -4) . '-' . stripslashes($imageType['name']) . '.jpg'; if (!file_exists($newDir . substr($image, 0, -4) . '-' . stripslashes($imageType['name']) . '.jpg')) { if (!file_exists($dir . $image) || !filesize($dir . $image)) $this->errors[] = sprintf(Tools::displayError('Source file does not exist or is empty (%s)'), $dir . $image); else { if (!ImageManager::resize($dir . $image, $newDir . substr($image, 0, -4) . '-' . stripslashes($imageType['name']) . '.jpg', (int)$imageType['width'], (int)$imageType['height'])) $this->errors[] = sprintf(Tools::displayError('Failed to resize image file (%s)'), $dir . $image); if ($generate_hight_dpi_images) { if (!ImageManager::resize($dir . $image, $newDir . substr($image, 0, -4) . '-' . stripslashes($imageType['name']) . '2x.jpg', (int)$imageType['width'] * 2, (int)$imageType['height'] * 2)) $this->errors[] = sprintf(Tools::displayError('Failed to resize image file to high resolution (%s)'), $dir . $image); } } } } } else { foreach (Image::getAllImages() as $image) { $imageObj = new Image($image['id_image']); $existing_img = $dir . $imageObj->getExistingImgPath() . '.jpg'; if (file_exists($existing_img) && filesize($existing_img)) { foreach ($type as $imageType) if (!file_exists($dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.jpg')) { echo "\n" . $dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.jpg'; if (!ImageManager::resize($existing_img, $dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.jpg', (int)$imageType['width'], (int)$imageType['height'])) $this->errors[] = sprintf(Tools::displayError('Original image is corrupt (%s) for product ID %2$d or bad permission on folder'), $existing_img, (int)$imageObj->id_product); if ($generate_hight_dpi_images) { if (!ImageManager::resize($existing_img, $dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '2x.jpg', (int)$imageType['width'] * 2, (int)$imageType['height'] * 2)) $this->errors[] = sprintf(Tools::displayError('Original image is corrupt (%s) for product ID %2$d or bad permission on folder'), $existing_img, (int)$imageObj->id_product); } } } else $this->errors[] = sprintf(Tools::displayError('Original image is missing or empty (%1$s) for product ID %2$d'), $existing_img, (int)$imageObj->id_product); } } return (bool)count($this->errors); } protected function _regenerateWatermark($dir, $type = null) { $result = Db::getInstance()->executeS(' SELECT m.`name` FROM `' . _DB_PREFIX_ . 'module` m LEFT JOIN `' . _DB_PREFIX_ . 'hook_module` hm ON hm.`id_module` = m.`id_module` LEFT JOIN `' . _DB_PREFIX_ . 'hook` h ON hm.`id_hook` = h.`id_hook` WHERE h.`name` = \'actionWatermark\' AND m.`active` = 1'); if ($result && count($result)) { $productsImages = Image::getAllImages(); foreach ($productsImages as $image) { $imageObj = new Image($image['id_image']); if (file_exists($dir . $imageObj->getExistingImgPath() . '.jpg')) foreach ($result as $module) { $moduleInstance = Module::getInstanceByName($module['name']); if ($moduleInstance && is_callable(array($moduleInstance, 'hookActionWatermark'))) call_user_func(array($moduleInstance, 'hookActionWatermark'), array('id_image' => $imageObj->id, 'id_product' => $imageObj->id_product, 'image_type' => $type)); } } } } } $admin = new CommandRegenerateImage(); -
michaelhoang revised this gist
Aug 19, 2015 . 1 changed file with 84 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,84 @@ <?php // Place file in admin folder // > php command-generate-image.php to run $timer_start = microtime(true); if (!defined('_PS_ADMIN_DIR_')) define('_PS_ADMIN_DIR_', getcwd()); if (!defined('PS_ADMIN_DIR')) define('PS_ADMIN_DIR', _PS_ADMIN_DIR_); require(_PS_ADMIN_DIR_ . '/../config/config.inc.php'); require(_PS_ADMIN_DIR_ . '/functions.php'); class CommandRegenerateImage extends AdminImagesControllerCore { public function regenerateThumbnails($type = 'all', $deleteOldImages = true) { $this->start_time = time(); ini_set('max_execution_time', $this->max_execution_time); // ini_set may be disabled, we need the real value $this->max_execution_time = (int)ini_get('max_execution_time'); $languages = Language::getLanguages(false); $process = array( array('type' => 'categories', 'dir' => _PS_CAT_IMG_DIR_), array('type' => 'manufacturers', 'dir' => _PS_MANU_IMG_DIR_), array('type' => 'suppliers', 'dir' => _PS_SUPP_IMG_DIR_), array('type' => 'scenes', 'dir' => _PS_SCENE_IMG_DIR_), array('type' => 'products', 'dir' => _PS_PROD_IMG_DIR_), array('type' => 'stores', 'dir' => _PS_STORE_IMG_DIR_) ); // Launching generation process foreach ($process as $proc) { if ($type != 'all' && $type != $proc['type']) continue; // Getting format generation $formats = ImageType::getImagesTypes($proc['type']); if ($type != 'all') { $format = strval(Tools::getValue('format_'.$type)); if ($format != 'all') foreach ($formats as $k => $form) if ($form['id_image_type'] != $format) unset($formats[$k]); } if ($deleteOldImages) $this->_deleteOldImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false)); if (($return = $this->_regenerateNewImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false))) === true) { if (!count($this->errors)) $this->errors[] = sprintf(Tools::displayError('Cannot write images for this type: %s. Please check the %s folder\'s writing permissions.'), $proc['type'], $proc['dir']); } elseif ($return == 'timeout') $this->errors[] = Tools::displayError('Only part of the images have been regenerated. The server timed out before finishing.'); else { if ($proc['type'] == 'products') if ($this->_regenerateWatermark($proc['dir']) == 'timeout') $this->errors[] = Tools::displayError('Server timed out. The watermark may not have been applied to all images.'); if (!count($this->errors)) if ($this->_regenerateNoPictureImages($proc['dir'], $formats, $languages)) $this->errors[] = sprintf( Tools::displayError('Cannot write "No picture" image to (%s) images folder. Please check the folder\'s writing permissions.'), $proc['type'] ); } // Processing echo "="; } if (count($this->errors)) { foreach ($this->errors as $error) { echo "\n" . $error; } } return (count($this->errors) > 0 ? false : true); } } $admin = new CommandRegenerateImage(); $admin->regenerateThumbnails(); -
michaelhoang created this gist
Aug 19, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ // All about prestashop