Jump to content

how to auto regenerate images (thumbnails) with theme install


Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...