Exemplo n.º 1
0
/**
 * liberty_generate_thumbnails
 *
 * @param array $pFileHash
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_generate_thumbnails($pFileHash)
{
    global $gBitSystem, $gThumbSizes;
    $resizeFunc = liberty_get_function('resize');
    $ret = FALSE;
    // allow custom selection of thumbnail sizes
    if (empty($pFileHash['thumbnail_sizes'])) {
        if (!empty($gThumbSizes) && is_array($gThumbSizes)) {
            $pFileHash['thumbnail_sizes'] = array_keys($gThumbSizes);
        } else {
            $pFileHash['thumbnail_sizes'] = array('large', 'medium', 'small', 'avatar', 'icon');
        }
    }
    if (!preg_match('#image/(gif|jpe?g|png)#i', $pFileHash['type']) && $gBitSystem->isFeatureActive('liberty_jpeg_originals') || in_array('original', $pFileHash['thumbnail_sizes'])) {
        // jpeg version of original
        if (preg_match('/pdf/i', $pFileHash['type'])) {
            // has a customer pdf rasterization function been defined?
            if (function_exists('liberty_rasterize_pdf') && ($rasteredFile = liberty_rasterize_pdf($pFileHash['source_file']))) {
                $pFileHash['source_file'] = $rasteredFile;
            } else {
                $magickWand = NewMagickWand();
                if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand))) {
                    MagickSetFormat($magickWand, 'JPG');
                    if (MagickGetImageColorspace($magickWand) == MW_CMYKColorspace) {
                        MagickProfileImage($magickWand, "ICC", UTIL_PKG_PATH . 'icc/srgb.icm');
                        MagickSetImageColorspace($magickWand, MW_sRGBColorspace);
                    }
                    $imgWidth = MagickGetImageWidth($magickWand);
                    $imgHeight = MagickGetImageHeight($magickWand);
                    MagickSetImageUnits($magickWand, MW_PixelsPerInchResolution);
                    MagickSetResolution($magickWand, 300, 300);
                    $rasteredFile = dirname($pFileHash['source_file']) . '/original.jpg';
                    if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickWriteImage($magickWand, $rasteredFile), $magickWand))) {
                        $pFileHash['source_file'] = $rasteredFile;
                    }
                }
            }
        } else {
            $pFileHash['dest_base_name'] = 'original';
            $pFileHash['name'] = 'original.jpg';
            $pFileHash['max_width'] = MAX_THUMBNAIL_DIMENSION;
            $pFileHash['max_height'] = MAX_THUMBNAIL_DIMENSION;
            if ($convertedFile = $resizeFunc($pFileHash)) {
                $pFileHash['source_file'] = $convertedFile;
                $ret = TRUE;
            }
        }
        $pFileHash['type'] = $gBitSystem->verifyMimeType($pFileHash['source_file']);
    }
    // override $mimeExt if we have a custom setting for it
    if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) {
        $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format');
    } else {
        list($type, $mimeExt) = preg_split('#/#', strtolower($pFileHash['type']));
    }
    if (preg_match("!(png|gif)!", $mimeExt)) {
        $destExt = '.' . $mimeExt;
    } else {
        $destExt = '.jpg';
    }
    $initialDestPath = $pFileHash['dest_branch'];
    foreach ($pFileHash['thumbnail_sizes'] as $thumbSize) {
        if (isset($gThumbSizes[$thumbSize])) {
            $pFileHash['dest_base_name'] = $thumbSize;
            $pFileHash['name'] = $thumbSize . $destExt;
            if (!empty($gThumbSizes[$thumbSize]['width'])) {
                $pFileHash['max_width'] = $gThumbSizes[$thumbSize]['width'];
            } else {
                // Have to unset since we reuse $pFileHash
                unset($pFileHash['max_width']);
            }
            // reset dest_branch for created thumbs
            if (!empty($pFileHash['thumb_path'])) {
                $pFileHash['dest_file'] = $pFileHash['thumb_path'] . $pFileHash['name'];
            } else {
                // create a subdirectory for the thumbs
                $pFileHash['dest_branch'] = $initialDestPath . 'thumbs/';
                clearstatcache();
                if (!is_dir(STORAGE_PKG_PATH . $pFileHash['dest_branch'])) {
                    mkdir(STORAGE_PKG_PATH . $pFileHash['dest_branch'], 0775, TRUE);
                    clearstatcache();
                }
            }
            if (!empty($gThumbSizes[$thumbSize]['height'])) {
                $pFileHash['max_height'] = $gThumbSizes[$thumbSize]['height'];
            } else {
                // Have to unset since we reuse $pFileHash
                unset($pFileHash['max_height']);
            }
            if ($pFileHash['icon_thumb_path'] = $resizeFunc($pFileHash)) {
                $ret = TRUE;
                // use the previous thumb as the source for the next, decreasingly smaller thumb as this GREATLY increases speed
                $pFileHash['source_file'] = $pFileHash['icon_thumb_path'];
            }
        }
    }
    // to keep everything in bitweaver working smoothly, we need to remove the thumbs/ subdir again
    $pFileHash['dest_branch'] = $initialDestPath;
    return $ret;
}
Exemplo n.º 2
0
 /**
  * @return Imagens
  * @param string $digital
  * @param string $md5
  * @param boolean $high
  * @param int $active
  * @param int $total
  */
 public function createCacheJpegView($digital, $md5, $high = false)
 {
     $iImageHeightPixel = $high === false ? 960 : 2480;
     $iImageWidthPixel = $high === false ? 1280 : 3508;
     $lote = $this->generateLote($digital);
     $dirCache = sprintf('%s/cache/%s', __CAM_UPLOAD__, $lote);
     $tiff = sprintf('%s/%s/%s/%s.tif', __CAM_UPLOAD__, $lote, $digital, $md5);
     $view = sprintf('%s/%s/%s_view_%d.jpg', $dirCache, $digital, $md5, $high === false ? self::Q_LOW : self::Q_HIGH);
     $thumbs = sprintf('%s/%s/%s_thumb.jpg', $dirCache, $digital, $md5);
     if (!is_file($tiff)) {
         throw new Exception('Arquivo TIFF original não encontrado!');
     }
     if (is_file($view)) {
         return $this;
     }
     // thumbs
     if (!is_file($thumbs)) {
         MagickReadImage($magickThumbs = NewMagickWand(), $tiff);
         if (MagickGetImageWidth($magickThumbs) < MagickGetImageHeight($magickThumbs)) {
             MagickResizeImage($magickThumbs, 150, 200, MW_QuadraticFilter, 1.0);
         } else {
             MagickResizeImage($magickThumbs, 200, 150, MW_QuadraticFilter, 1.0);
         }
         MagickSetImageFormat($magickThumbs, 'JPG');
         MagickSetImageResolution($magickThumbs, 200, 200);
         MagickSetImageUnits($magickThumbs, MW_PixelsPerInchResolution);
         MagickSetImageCompression($magickThumbs, MW_JPEGCompression);
         MagickSetImageCompressionQuality($magickThumbs, 0.0);
         MagickWriteImage($magickThumbs, $thumbs);
     }
     // views
     MagickReadImage($magickView = NewMagickWand(), $tiff);
     if (MagickGetImageWidth($magickView) > MagickGetImageHeight($magickView)) {
         MagickResizeImage($magickView, $iImageWidthPixel, $iImageHeightPixel, MW_QuadraticFilter, 1.0);
     } else {
         MagickResizeImage($magickView, $iImageHeightPixel, $iImageWidthPixel, MW_QuadraticFilter, 1.0);
     }
     MagickSetImageFormat($magickView, 'JPG');
     MagickSetImageResolution($magickView, 200, 200);
     MagickSetImageUnits($magickView, MW_PixelsPerInchResolution);
     MagickSetImageCompression($magickView, MW_JPEGCompression);
     MagickSetImageCompressionQuality($magickView, 0.0);
     MagickWriteImage($magickView, $view);
     $errorMagick = MagickGetExceptionString($magickView);
     if ($errorMagick) {
         throw new Exception($errorMagick);
     }
     return $this;
 }