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.
All about Prestashop
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 characters
<?php | |
$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) | |
{ | |
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(); | |
$admin->regenerateThumbnails(); |
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 characters
// All about prestashop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment