Example #1
0
 /**
  * nggAdmin::createThumbnail() - function to create or recreate a thumbnail
  * 
  * @param object | int $image contain all information about the image or the id
  * @return string result code
  * @since v1.0.0
  */
 function create_thumbnail($image)
 {
     global $ngg;
     if (!class_exists('ngg_Thumbnail')) {
         require_once nggGallery::graphic_library();
     }
     if (is_numeric($image)) {
         $image = nggdb::find_image($image);
     }
     if (!is_object($image)) {
         return __('Object didn\'t contain correct data', 'nggallery');
     }
     // check for existing thumbnail
     if (file_exists($image->thumbPath)) {
         if (!is_writable($image->thumbPath)) {
             return $image->filename . __(' is not writeable ', 'nggallery');
         }
     }
     $thumb = new ngg_Thumbnail($image->imagePath, TRUE);
     // skip if file is not there
     if (!$thumb->error) {
         if ($ngg->options['thumbcrop']) {
             // THX to Kees de Bruin, better thumbnails if portrait format
             $width = $ngg->options['thumbwidth'];
             $height = $ngg->options['thumbheight'];
             $curwidth = $thumb->currentDimensions['width'];
             $curheight = $thumb->currentDimensions['height'];
             if ($curwidth > $curheight) {
                 $aspect = 100 * $curwidth / $curheight;
             } else {
                 $aspect = 100 * $curheight / $curwidth;
             }
             $width = round($width * $aspect / 100);
             $height = round($height * $aspect / 100);
             $thumb->resize($width, $height);
             $thumb->cropFromCenter($width);
         } elseif ($ngg->options['thumbfix']) {
             // check for portrait format
             if ($thumb->currentDimensions['height'] > $thumb->currentDimensions['width']) {
                 $thumb->resize($ngg->options['thumbwidth'], 0);
                 // get optimal y startpos
                 $ypos = ($thumb->currentDimensions['height'] - $ngg->options['thumbheight']) / 2;
                 $thumb->crop(0, $ypos, $ngg->options['thumbwidth'], $ngg->options['thumbheight']);
             } else {
                 $thumb->resize(0, $ngg->options['thumbheight']);
                 // get optimal x startpos
                 $xpos = ($thumb->currentDimensions['width'] - $ngg->options['thumbwidth']) / 2;
                 $thumb->crop($xpos, 0, $ngg->options['thumbwidth'], $ngg->options['thumbheight']);
             }
         } else {
             $thumb->resize($ngg->options['thumbwidth'], $ngg->options['thumbheight']);
         }
         // save the new thumbnail
         $thumb->save($image->thumbPath, $ngg->options['thumbquality']);
         nggAdmin::chmod($image->thumbPath);
     }
     $thumb->destruct();
     if (!empty($thumb->errmsg)) {
         return ' <strong>' . $image->filename . ' (Error : ' . $thumb->errmsg . ')</strong>';
     }
     // success
     return '1';
 }