Products & Downloads » Prestashop hacks » Image Regeneration (new only)
Prestashop image regeneration feature is wonderful but can be long to process for more than a few dozen images. The hack below creates a new regenerate button that does not delete existing generated thumbnails and creates thumbnails for new imported images.
HowTO
| public function displayRegenerate() { global $currentIndex; echo ' <h2 class="space">'.$this->l('Regenerate thumbnails').'</h2> '.$this->l('Regenerates thumbnails for all existing product images').'.<br /><br />'; $this->displayWarning($this->l('Please be patient, as this can take several minutes').'<br />'.$this->l('Be carefull !! Manual generated thumbnails will be erased by automatic generation thumbnails.')); echo ' <form action="'.$currentIndex.'&token='.$this->token.'" method="post"> <input type="Submit" name="submitRegenerate'.$this->table.'" value="'.$this->l('Regenerate thumbnails').'" class="button space" confirm(\''.addslashes($this->l('Are you sure?')).'\');" /> </form>'; echo ' <form action="'.$currentIndex.'&token='.$this->token.'" method="post"> <input type="Submit" name="submitRegenerateNew'.$this->table.'" value="'.$this->l('Regenerate thumbnails for new images').'" class="button space" you sure?')).'\');" /> </form>'; } |
| private function _regenerateThumbnails($new_only=false) { $productsTypes = ImageType::getImagesTypes('products'); $categoriesTypes = ImageType::getImagesTypes('categories'); $languages = Language::getLanguages(); /* Delete products images */ if (!$new_only) { $toDel = scandir(_PS_PROD_IMG_DIR_); foreach ($toDel AS $d) if (ereg('^[0-9]+\-[0-9]+\-(.*)\.jpg$', $d) OR ereg('^([[:lower:]]{2})\-default\-(.*)\.jpg$', $d)) unlink(_PS_PROD_IMG_DIR_.$d); } $productsImages = Image::getAllImages(); /* Regenerate products images */ $errors = false; foreach ($productsImages AS $k => $image) { if (file_exists(_PS_PROD_IMG_DIR_.$image['id_product'].'-'.$image['id_image'].'.jpg')) foreach ($productsTypes AS $k => $imageType) { $file['tmp_name'] = _PS_PROD_IMG_DIR_.$image['id_product'].'-'.$image['id_image'].'.jpg'; $file['type'] = 'image/jpg'; $newFile = _PS_PROD_IMG_DIR_.$image['id_product'].'-'.$image['id_image'].'-'.stripslashes($imageType['name']).'.jpg'; if (!$new_only || !file_exists($newFile)) { if (!imageResize($file, $newFile, intval($imageType['width']), intval($imageType['height']))) $errors = true; } } } |
Screenshot