xezus Posted July 24, 2013 Share Posted July 24, 2013 Hi, I would like to auto regenerate theme images with new theme install,so to remove the need to do this manually. But I can't find any way how do that. Could anybody point me with the right direction? Link to comment Share on other sites More sharing options...
vekia Posted July 25, 2013 Share Posted July 25, 2013 you have to regenerate them manually under the preferences > images tab in your back office. as far as i know there is no other way Link to comment Share on other sites More sharing options...
bellini13 Posted July 25, 2013 Share Posted July 25, 2013 well, the way is to locate the function that is called when you click the "regenerate image" button. Then you would have to alter the theme installer module to execute that function Link to comment Share on other sites More sharing options...
vekia Posted July 25, 2013 Share Posted July 25, 2013 that's right, i totally agree with bellini13, moreover, in my opinion this is the only way to achieve this Link to comment Share on other sites More sharing options...
xezus Posted July 26, 2013 Author Share Posted July 26, 2013 Hi bellini13, thanks for your advice, but could you be pls more specific. Do you know where can I find this function? And would this function also change image sizes? I mean when I have different image size settings, than default theme. Link to comment Share on other sites More sharing options...
PascalVG Posted July 26, 2013 Share Posted July 26, 2013 Hi xezus, Function is called and defined in /controllers/admin/AdminImagesController.php When button is pressed, in function: public function postProcess() the function $this->_regenerateThumbnails(Tools::getValue('type'), Tools::getValue('erase')) is called. this function is defined in the same file: protected function _regenerateThumbnails($type = 'all', $deleteOldImages = false) { $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 %s images. Please check the folder\'s writing permissions %s.'), $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'] ); } } return (count($this->errors) > 0 ? false : true); } In this code, the function: $this->_regenerateNewImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false)) is called, doing the actual regenerating of the pictures. This function is also defined in this file: /** * Regenerate images * * @param $dir * @param $type * @param bool $productsImages * @return bool|string */ protected function _regenerateNewImages($dir, $type, $productsImages = false) { if (!is_dir($dir)) return false; $errors = false; 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; if (!file_exists($newDir.substr($image, 0, -4).'-'.stripslashes($imageType['name']).'.jpg')) { if (!file_exists($dir.$image) || !filesize($dir.$image)) { $errors = true; $this->errors[] = sprintf(Tools::displayError('Source file does not exist or is empty (%s)', $dir.$image)); } elseif (!ImageManager::resize($dir.$image, $newDir.substr($image, 0, -4).'-'.stripslashes($imageType['name']).'.jpg', (int)$imageType['width'], (int)$imageType['height'])) $errors = true; } if (time() - $this->start_time > $this->max_execution_time - 4) // stop 4 seconds before the tiemout, just enough time to process the end of the page on a slow server return 'timeout'; } } 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')) if (!ImageManager::resize($existing_img, $dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']))) { $errors = true; $this->errors[] = Tools::displayError(sprintf('Original image is corrupt (%s) or bad permission on folder', $existing_img)); } } else { $errors = true; $this->errors[] = Tools::displayError(sprintf('Original image is missing or empty (%s)', $existing_img)); } } } return $errors; } As you can see in this code, it resizes the pictures with the current size values. So if you make sure you change the needed values beforehand, it should be resized accordingly. Hope this helps, pascal Link to comment Share on other sites More sharing options...
xezus Posted August 1, 2013 Author Share Posted August 1, 2013 Sorry for delay answer. Yes thank you pascalIVG, this is what I was looking for. Link to comment Share on other sites More sharing options...
PascalVG Posted August 1, 2013 Share Posted August 1, 2013 Can I mark the Topic solved? Or do you still have some questions about it? 1 Link to comment Share on other sites More sharing options...
xezus Posted August 1, 2013 Author Share Posted August 1, 2013 Yes you can. I added this task to my distant future to do list Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now