/**
  * Generate a layout thumbnail according to the configuration.
  *
  * @access public
  *
  * @param Layout        $layout The layout to treate
  * @param BBApplication $app    The current instance of BBApplication
  *
  * @return mixed FALSE if something wrong, the ressource path of the thumbnail elsewhere
  */
 public function generateThumbnail(Layout $layout, BBApplication $app)
 {
     // Is the layout valid ?
     if (!$layout->isValid()) {
         return false;
     }
     // Is some layout configuration existing ?
     if (null === $app->getConfig()->getSection('layout')) {
         return false;
     }
     $layoutconfig = $app->getConfig()->getSection('layout');
     // Is some thumbnail configuration existing ?
     if (!isset($layoutconfig['thumbnail'])) {
         return false;
     }
     $thumbnailconfig = $layoutconfig['thumbnail'];
     // Is gd available ?
     if (!function_exists('gd_info')) {
         return false;
     }
     $gd_info = gd_info();
     // Is the selected format supported by gd ?
     if (!isset($thumbnailconfig['format'])) {
         return false;
     }
     if (true !== $gd_info[strtoupper($thumbnailconfig['format']) . ' Support']) {
         return false;
     }
     // Is the template file existing ?
     if (!isset($thumbnailconfig['template'])) {
         return false;
     }
     $templatefile = $thumbnailconfig['template'];
     $thumbnaildir = dirname($templatefile);
     File::resolveFilepath($templatefile, null, array('include_path' => $app->getResourceDir()));
     if (false === file_exists($templatefile) || false === is_readable($templatefile)) {
         return false;
     }
     try {
         $gd_function = 'imagecreatefrom' . strtolower($thumbnailconfig['format']);
         $thumbnail = $gd_function($templatefile);
         $thumbnailfile = $thumbnaildir . '/' . $layout->getUid() . '.' . strtolower($thumbnailconfig['format']);
         // Is a background color existing ?
         if (!isset($thumbnailconfig['background']) || !is_array($thumbnailconfig['background']) || 3 != count($thumbnailconfig['background'])) {
             return false;
         }
         $background = imagecolorallocate($thumbnail, $thumbnailconfig['background'][0], $thumbnailconfig['background'][1], $thumbnailconfig['background'][2]);
         // Is a clipping zone existing ?
         if (!isset($thumbnailconfig['clip']) || !is_array($thumbnailconfig['clip']) || 4 != count($thumbnailconfig['clip'])) {
             return false;
         }
         $gridcolumn = 12;
         if (null !== ($lessconfig = $app->getConfig()->getSection('less'))) {
             if (isset($lessconfig['gridcolumn'])) {
                 $gridcolumn = $lessconfig['gridcolumn'];
             }
         }
         $domlayout = $layout->getDomDocument();
         if (!$domlayout->hasChildNodes() || !$domlayout->firstChild->hasChildNodes()) {
             $this->drawRect($thumbnail, $thumbnailconfig['clip'], $background);
         } else {
             $this->drawThumbnailZone($thumbnail, $domlayout->firstChild, $thumbnailconfig['clip'], $background, $gridcolumn);
         }
         imagesavealpha($thumbnail, true);
         $thumbnaildir = dirname(File::normalizePath($app->getCurrentResourceDir() . '/' . $thumbnailfile));
         if (false === is_dir($thumbnaildir)) {
             mkdir($thumbnaildir, 0755, true);
         }
         imagepng($thumbnail, File::normalizePath($app->getCurrentResourceDir() . '/' . $thumbnailfile));
     } catch (\Exception $e) {
         return false;
     }
     $layout->setPicPath($thumbnailfile);
     return $layout->getPicPath();
 }