Ejemplo n.º 1
0
 function resize($img, $options, $folder = "", $cache = true)
 {
     $hash = md5($img . serialize($options));
     if (!file_exists($img) || is_dir($img)) {
         $img = PATH_DESIGN . "img" . DS . "nophoto.png";
     }
     $ext = "";
     $phpThumb = new \phpthumb();
     $phpThumb->setSourceFilename($img);
     $phpThumb->setParameter("q", IMAGE_QUALITY_DEFAULT);
     foreach ($options as $key => $value) {
         $phpThumb->setParameter($key, $value);
         if ($key == "f") {
             $ext = $value;
         }
     }
     if ($ext == "") {
         $phpThumb->setParameter("f", "jpg");
         $ext = "jpg";
     }
     if ($folder != "") {
         $tree = explode("/", $folder);
         $path = IMAGE_CACHE_PATH;
         if (is_array($tree)) {
             foreach ($tree as $dir) {
                 $mk = $path . DS . $dir;
                 if (!file_exists($mk)) {
                     mkdir($mk);
                     chmod($mk, 0777);
                 }
                 $path .= DS . $dir;
             }
         }
         $folder .= DS;
     }
     $outputFilename = IMAGE_CACHE_PATH . $folder . $hash . "." . $ext;
     if (!$cache && file_exists($outputFilename)) {
         unlink($outputFilename);
     }
     if (!file_exists($outputFilename)) {
         if ($phpThumb->GenerateThumbnail()) {
             $phpThumb->RenderToFile($outputFilename);
         }
     }
     $res = str_replace(ROOT, "", $outputFilename);
     $res = str_replace(DS, "/", $res);
     return $res;
 }
Ejemplo n.º 2
0
 protected function _thumb($sourceFilename, $targetFilename, $params)
 {
     // recursively create folders
     new Folder(dirname($targetFilename), true);
     // Import phpThumb class
     App::import('Vendor', 'phpthumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     // Configuring thumbnail settings
     $phpThumb = new phpthumb();
     $phpThumb->setSourceFilename($sourceFilename);
     $phpThumb->w = $params['width'];
     $phpThumb->h = $params['height'];
     $phpThumb->setParameter('zc', $params['zoomCrop']);
     $phpThumb->q = $params['quality'];
     $imageArray = explode(".", $source);
     $phpThumb->config_output_format = $imageArray[1];
     unset($imageArray);
     // Setting whether to die upon error
     $phpThumb->config_error_die_on_error = true;
     // Creating thumbnail
     if ($phpThumb->GenerateThumbnail()) {
         if (!$phpThumb->RenderToFile($targetFilename)) {
             trigger_error('Could not render image to: ' . $targetFilename);
         }
     }
 }
Ejemplo n.º 3
0
 function index()
 {
     if (count($this->params['pass'])) {
         $src = implode('/', array_slice($this->params['pass'], 1));
         $action = $this->params['pass'][0];
         $phpThumb = new phpthumb();
         $sourceFilename = Configure::read('Resource.media_path') . DS . $src;
         $cacheFilename = md5("{$action}{$src}");
         $phpThumb->src = $sourceFilename;
         $phpThumb->config_cache_directory = Configure::read('Thumb.cache_path') . DS;
         $phpThumb->config_imagemagick_path = Configure::read('Thumb.imagemagick_path');
         $phpThumb->config_prefer_imagemagick = true;
         $phpThumb->config_output_format = 'jpeg';
         $phpThumb->config_error_die_on_error = false;
         $phpThumb->config_document_root = '';
         $phpThumb->config_cache_prefix = '';
         $phpThumb->config_cache_source_enabled = 1;
         $phpThumb->cache_filename = $phpThumb->config_cache_directory . $cacheFilename;
         $phpThumb->config_max_source_pixels = 5000000;
         $phpThumb->sia = implode('_', array_slice($this->params['pass'], 1));
         $thumb_parameters = Configure::read('Thumb.media_parameters');
         if (in_array($action, array_keys($thumb_parameters))) {
             $thumb_vars = $thumb_parameters[$action];
             $phpThumb->cache_filename = $phpThumb->config_cache_directory . $cacheFilename;
             foreach ($thumb_vars as $ti => $thumb_var) {
                 $phpThumb->setParameter($ti, $thumb_var);
             }
             if (file_exists($phpThumb->cache_filename) && filemtime($phpThumb->src) < filemtime($phpThumb->cache_filename)) {
                 $phpThumb->useRawIMoutput = true;
                 $phpThumb->IMresizedData = file_get_contents($phpThumb->cache_filename);
                 $this->set('phpthumb', $phpThumb);
             } elseif ($phpThumb->GenerateThumbnail()) {
                 $phpThumb->RenderToFile($phpThumb->cache_filename);
                 $this->set('phpthumb', $phpThumb);
             } else {
                 die('Error caching thumbnail <pre>' . print_r($phpThumb, true) . '</pre>');
             }
         } else {
             $this->redirect(Configure::read('Resource.media_path') . "/{$src}");
         }
     } else {
         $this->viewPath = 'errors';
         $this->render('not_found');
     }
 }
 /**
  * Function to create Thumbnail images
  *
  * @param object $model Reference to model
  * @param string $source File name (without path)
  * @param string $target File name (without path)
  * @param string $fieldName
  * @param array $params
  * @return void
  * @access protected
  */
 function _createThumbnail(&$model, $source, $target, $fieldName, $params = array())
 {
     $params = array_merge(array('thumbWidth' => 150, 'thumbHeight' => 225, 'maxDimension' => '', 'thumbnailQuality' => $this->__fields[$model->alias][$fieldName]['thumbnailQuality'], 'zoomCrop' => false), $params);
     // Import phpThumb class
     $test = App::import('Vendor', 'phpthumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     // Configuring thumbnail settings
     $phpThumb = new phpthumb();
     $phpThumb->setSourceFilename($source);
     $phpThumb->config_disable_debug = !Configure::read('debug');
     if ($params['maxDimension'] == 'w') {
         $phpThumb->w = $params['thumbWidth'];
     } else {
         if ($params['maxDimension'] == 'h') {
             $phpThumb->h = $params['thumbHeight'];
         } else {
             $phpThumb->w = $params['thumbWidth'];
             $phpThumb->h = $params['thumbHeight'];
         }
     }
     $phpThumb->setParameter('zc', $this->__fields[$model->alias][$fieldName]['zoomCrop']);
     if (isset($params['zoomCrop'])) {
         $phpThumb->setParameter('zc', $params['zoomCrop']);
     }
     if (isset($params['watermark'])) {
         $phpThumb->fltr = array("wmi|" . IMAGES . $params['watermark'] . "|BR|50|5");
     }
     $phpThumb->q = $params['thumbnailQuality'];
     $imageArray = explode(".", $source);
     $phpThumb->config_output_format = $imageArray[1];
     unset($imageArray);
     $phpThumb->config_prefer_imagemagick = $this->__fields[$model->alias][$fieldName]['useImageMagick'];
     $phpThumb->config_imagemagick_path = $this->__fields[$model->alias][$fieldName]['imageMagickPath'];
     // Setting whether to die upon error
     $phpThumb->config_error_die_on_error = true;
     // Creating thumbnail
     if ($phpThumb->GenerateThumbnail()) {
         if (!$phpThumb->RenderToFile($target)) {
             $this->_addError('Could not render image to: ' . $target);
         }
     }
 }
Ejemplo n.º 5
0
 function generateThumbnail($saveAs, $options)
 {
     $destination = WWW_ROOT . $options['directory'] . DS . basename($saveAs);
     $ext = substr(basename($saveAs), strrpos(basename($saveAs), '.') + 1);
     if ($ext == '.jpg' || $ext == '.jpeg') {
         $format = 'jpeg';
     } elseif ($ext == 'png') {
         $format = 'png';
     } elseif ($ext == 'gif') {
         $format = 'gif';
     } else {
         $format = 'jpeg';
     }
     $phpThumb = new phpthumb();
     $phpThumb->setSourceFilename($saveAs);
     $phpThumb->setCacheDirectory(CACHE);
     $phpThumb->setParameter('w', $options['width']);
     if (!empty($option['height'])) {
         $phpThumb->setParameter('h', $options['height']);
     }
     $phpThumb->setParameter('f', $format);
     if (!empty($options['phpThumb'])) {
         foreach ($options['phpThumb'] as $name => $value) {
             if (!empty($value)) {
                 $phpThumb->setParameter($name, $value);
             }
         }
     }
     if ($phpThumb->generateThumbnail()) {
         if ($phpThumb->RenderToFile($destination)) {
             chmod($destination, 0644);
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Ejemplo n.º 6
0
$filename = md5($img . $options);
$cache = isset($cache) ? $cache : false;
if ($img[0] != "/") {
    $img = "/" . $img;
}
$nophoto = isset($nophoto) ? $nophoto : "logo.png";
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $img) || is_dir($_SERVER['DOCUMENT_ROOT'] . $img)) {
    $img = "/images/" . $nophoto;
}
require_once dirname(__FILE__) . "/phpthumb.class.php";
$phpThumb = new phpthumb();
$phpThumb->setSourceFilename($img);
$options = explode("&", $options);
foreach ($options as $value) {
    $thumb = explode("=", $value);
    $phpThumb->setParameter($thumb[0], $thumb[1]);
    if ($thumb[0] == "f") {
        $ext = $thumb[1];
    }
}
if ($ext == "") {
    $phpThumb->setParameter("f", "jpg");
    $ext = "jpg";
}
if ($folder != "") {
    $tree = explode("/", $folder);
    $path = $_SERVER['DOCUMENT_ROOT'] . "/assets/cache/images";
    if (is_array($tree)) {
        foreach ($tree as $dir) {
            $mk = $path . "/" . $dir;
            if (!file_exists($mk)) {
Ejemplo n.º 7
0
$tmpImagesFolder = str_replace("assets/images", "", $tmpImagesFolder);
$tmpImagesFolder = explode("/", $tmpImagesFolder);
$ext = strtolower($path_parts['extension']);
$options = 'f=' . (in_array($ext, explode(",", "png,gif")) ? $ext : "jpg&q=96") . '&' . strtr($options, array("," => "&", "_" => "=", '{' => '[', '}' => ']'));
parse_str($options, $params);
foreach ($tmpImagesFolder as $folder) {
    if (!empty($folder)) {
        $cacheFolder .= "/" . $folder;
        if (!is_dir(MODX_BASE_PATH . $cacheFolder)) {
            mkdir(MODX_BASE_PATH . $cacheFolder);
        }
    }
}
$fname_preffix = $cacheFolder . "/" . $params['w'] . "x" . $params['h'] . '-';
$fname = $path_parts['filename'] . "." . substr(md5(serialize($params)), 0, 3) . "." . $params['f'];
$outputFilename = MODX_BASE_PATH . $fname_preffix . $fname;
if (!file_exists($outputFilename)) {
    require_once MODX_BASE_PATH . 'assets/snippets/phpthumb/phpthumb.class.php';
    $phpThumb = new phpthumb();
    $phpThumb->config_temp_directory = $tmpFolder;
    $phpThumb->setSourceFilename(MODX_BASE_PATH . $input);
    foreach ($params as $key => $value) {
        $phpThumb->setParameter($key, $value);
    }
    if ($phpThumb->GenerateThumbnail()) {
        $phpThumb->RenderToFile($outputFilename);
    } else {
        $modx->logEvent(0, 3, implode('<br/>', $phpThumb->debugmessages), 'phpthumb');
    }
}
return $fname_preffix . rawurlencode($fname);
Ejemplo n.º 8
0
 /**
  * Function to create Thumbnail images
  *
  * @param object $model
  * @param string $source Source file name (without path)
  * @param string $target Target file name (without path)
  * @param string $fieldName Path to source and destination (no trailing DS)
  * @param array $params
  * @return void
  * @access protected
  */
 function _createThumbnail(&$model, $source, $target, $fieldName, $params = array())
 {
     $params = array_merge(array('thumbnailQuality' => $this->__fields[$model->alias][$fieldName]['thumbnailQuality'], 'zoomCrop' => false), $params);
     // Import phpThumb class
     App::import('Vendor', 'phpthumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     // Configuring thumbnail settings
     $phpThumb = new phpthumb();
     $phpThumb->setSourceFilename($source);
     $w = isset($params['width']);
     $h = isset($params['height']);
     if ($w && $h) {
         $phpThumb->w = $params['width'];
         $phpThumb->h = $params['height'];
     } elseif ($w && !$h) {
         $phpThumb->w = $params['width'];
     } elseif ($h && !$w) {
         $phpThumb->h = $params['height'];
     } else {
         trigger_error(__d('meio_upload', 'Width and Height of thumbs not specified.', true), E_USER_WARNING);
         return;
     }
     $phpThumb->setParameter('zc', $params['zoomCrop']);
     $phpThumb->q = $params['thumbnailQuality'];
     list(, $phpThumb->config_output_format) = explode('.', $source, 2);
     $phpThumb->config_prefer_imagemagick = $this->__fields[$model->alias][$fieldName]['useImageMagick'];
     $phpThumb->config_imagemagick_path = $this->__fields[$model->alias][$fieldName]['imageMagickPath'];
     // Setting whether to die upon error
     $phpThumb->config_error_die_on_error = true;
     // Creating thumbnail
     if ($phpThumb->GenerateThumbnail()) {
         if (!$phpThumb->RenderToFile($target)) {
             trigger_error(sprintf(__d('meio_upload', 'Could not render image to: %s', true), $target), E_USER_WARNING);
         }
     }
 }
Ejemplo n.º 9
0
 /**
  * Secure PHP Thumb resizing
  */
 public function thumb($img, $properties)
 {
     $file = $img;
     $ext = end(explode('.', $img));
     $kx = '';
     foreach ($properties as $k => $v) {
         $kx .= $k . $v;
     }
     $dir = PUBLIC_DIR . "images/thumbs/";
     $thumb = $dir . hash_hmac('ripemd160', $img . $kx, 'sThumbz1') . '.' . $ext;
     if (!$properties['q']) {
         $properties['q'] = 100;
     }
     if (file_exists($file) && !file_exists($thumb)) {
         if (!is_dir($dir)) {
             mkdir($dir, 0755, true);
         }
         $phpThumb = new phpthumb();
         $PHPTHUMB_CONFIG = array('document_root' => ROOT_DIR, 'cache_directory' => ROOT_DIR . '../application/plugins/phpThumb/cache/', 'imagemagick_path' => 'C:/Program Files (x86)/ImageMagick-6.7.7-Q16/convert.exe', 'disable_debug' => false);
         foreach ($PHPTHUMB_CONFIG as $key => $value) {
             $keyname = 'config_' . $key;
             $phpThumb->setParameter($keyname, $value);
         }
         $phpThumb->setSourceFilename(ROOT_DIR . $file);
         foreach ($properties as $k => $v) {
             $phpThumb->setParameter($k, $v);
         }
         if ($gt = $phpThumb->GenerateThumbnail()) {
             if ($rt = $phpThumb->RenderToFile(ROOT_DIR . $thumb)) {
                 return $thumb;
             } else {
                 return $img;
             }
         } else {
             return $img;
         }
     } elseif (file_exists($thumb)) {
         /*
          * Thumb was already made!
          */
         return $thumb;
     } else {
         /*
          * Return the original input as no file was found in the ul folder
          */
         return $img;
     }
 }
Ejemplo n.º 10
0
 $options = strtr($options, $replace);
 $opt = $options;
 $path_parts = pathinfo($input);
 require_once MODX_BASE_PATH . "/assets/snippets/phpthumb/phpthumb.class.php";
 $phpThumb = new phpthumb();
 $phpThumb->setSourceFilename($input);
 $options = explode("&", $options);
 $allow = array('f' => 'jpg', 'q' => '96');
 $need = array_keys($allow);
 foreach ($options as $value) {
     $thumb = explode("=", $value);
     if (in_array($thumb[0], $need)) {
         $opt .= "&" . $thumb[0] . "=" . $thumb[1];
         unset($allow[$thumb[0]]);
     }
     $phpThumb->setParameter($thumb[0], $thumb[1]);
     $op[$thumb[0]] = $thumb[1];
 }
 foreach ($allow as $key => $value) {
     $opt .= "&" . $key . "=" . $value;
     $phpThumb->setParameter($key, $value);
     $op[$key] = $value;
 }
 $tmp = preg_replace("#^" . $_SERVER['DOCUMENT_ROOT'] . "assets/images/#", "", $input);
 $tmp = preg_replace("#^assets/images/#", "", $tmp);
 $tmp = preg_replace("#/" . $path_parts['basename'] . "\$#", "", $tmp);
 $ftime = filemtime($input);
 $tmp = "assets/cache/phpthumb/" . $tmp;
 $tmp = explode("/", $tmp);
 $tmp[] = md5($opt);
 $tmp[] = date("Y-m", $ftime);
Ejemplo n.º 11
0
 /**
  * Function to create Thumbnail images
  *
  * @param object $model Reference to model
  * @param string $source File name (without path)
  * @param string $target File name (without path)
  * @param string $fieldName
  * @param array $params
  * @return void
  * @access protected
  */
 function _createThumbnail(&$model, $source, $target, $fieldName, $params = array())
 {
     # zuha added to have a setting for whether to resize or crop
     $zoomCrop = defined('__GALLERY_RESIZE_OR_CROP') && __GALLERY_RESIZE_OR_CROP == 'crop' ? true : false;
     $params = array_merge(array('thumbWidth' => 150, 'thumbHeight' => 225, 'maxDimension' => '', 'thumbnailQuality' => $this->__fields[$model->alias][$fieldName]['thumbnailQuality'], 'zoomCrop' => $zoomCrop), $params);
     // Import phpThumb class
     //App::import('Vendor', 'Galleries.phpThumb', array('file' => 'phpThumb'.DS.'phpthumb.class.php'));
     require_once APP . 'Plugin' . DS . 'Galleries' . DS . 'vendors' . DS . 'phpThumb' . DS . 'phpthumb.class.php';
     // @todo import
     // Configuring thumbnail settings
     $phpThumb = new phpthumb();
     $phpThumb->setSourceFilename($source);
     if ($params['maxDimension'] == 'w') {
         $phpThumb->w = $params['thumbWidth'];
     } else {
         if ($params['maxDimension'] == 'h') {
             $phpThumb->h = $params['thumbHeight'];
         } else {
             $phpThumb->w = $params['thumbWidth'];
             $phpThumb->h = $params['thumbHeight'];
         }
     }
     $phpThumb->setParameter('zc', $this->__fields[$model->alias][$fieldName]['zoomCrop']);
     if (isset($params['zoomCrop'])) {
         $phpThumb->setParameter('zc', $params['zoomCrop']);
     }
     $phpThumb->q = $params['thumbnailQuality'];
     $imageArray = explode(".", $source);
     $phpThumb->config_output_format = $imageArray[1];
     unset($imageArray);
     $phpThumb->config_prefer_imagemagick = $this->__fields[$model->alias][$fieldName]['useImageMagick'];
     $phpThumb->config_imagemagick_path = $this->__fields[$model->alias][$fieldName]['imageMagickPath'];
     // Setting whether to die upon error
     $phpThumb->config_error_die_on_error = true;
     // Creating thumbnail
     if ($phpThumb->GenerateThumbnail()) {
         if (!$phpThumb->RenderToFile($target)) {
             $this->_addError('Could not render image to: ' . $target);
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * Create a thumbnail, and cache.
  *
  * @return mixed Cached filename returned if thumbnailing was successful. False on error.
  */
 public function createThumb($src, $dest)
 {
     App::import('Vendor', 'phpthumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     if (!class_exists('phpthumb')) {
         trigger_error(__('phpThumb must be installed in the vendors directory: APP/vendors/phpThumb', true), E_USER_ERROR);
     }
     $phpThumb = new phpthumb();
     $phpThumb->setParameter('src', $src);
     foreach ($this->options as $k => $v) {
         if ($k == 'fltr') {
             // Setup the Filtering as string options.
             foreach ($v as $vk => &$vv) {
                 $vv = $vk . '|' . $vv;
             }
             $phpThumb->setParameter($k, $v);
         } else {
             // All non-filter options
             $phpThumb->setParameter($k, $v);
         }
     }
     @ini_set('max_execution_time', 0);
     if ($phpThumb->GenerateThumbnail()) {
         $phpThumb->RenderToFile('img' . DS . $dest);
         return $dest;
     }
     $this->_error = preg_replace('/[^A-Za-z0-9\\/: .]/', '', $phpThumb->fatalerror);
     $this->_error = preg_replace('/phpThumb v[0-9\\.\\-]+/', '', $this->_error);
     return false;
 }
Ejemplo n.º 13
0
 * @version  SVN: $Id:\$
 * @link     http://www.mediboard.org
 */
include_once 'lib/phpThumb/phpThumb.config.php';
CAppUI::requireLibraryFile("phpThumb/phpthumb.class");
$file_id = CValue::get("file_id");
$index = CValue::get("index");
$file = new CFile();
$file->load($file_id);
// Si le pdf a été supprimé ou vidé car on ferme la popup sans enregistrer
// le document, alors on ne génère pas la vignette
if (!$file->_id || !file_exists($file->_file_path) || file_get_contents($file->_file_path) == "") {
    return;
}
// Traitement de la vignette
$thumbs = new phpthumb();
// Il faut inclure manuellement la configuration dans l'objet phpThumbs
if (!empty($PHPTHUMB_CONFIG)) {
    foreach ($PHPTHUMB_CONFIG as $key => $value) {
        $keyname = 'config_' . $key;
        $thumbs->setParameter($keyname, $value);
    }
}
$thumbs->setSourceFilename($file->_file_path);
$vignette = null;
$thumbs->sfn = $index;
$thumbs->w = 138;
$thumbs->f = "png";
$thumbs->GenerateThumbnail();
$vignette = base64_encode($thumbs->IMresizedData);
echo json_encode($vignette);
Ejemplo n.º 14
0
    $config['modphpthumb'] = array();
    $config['modphpthumb']['config_allow_src_above_docroot'] = (bool) $modx->getOption('phpthumb_allow_src_above_docroot', null, false);
    $config['modphpthumb']['zc'] = $modx->getOption('phpthumb_zoomcrop', null, 0);
    $config['modphpthumb']['far'] = $modx->getOption('phpthumb_far', null, 'C');
    $config['modphpthumb']['config_ttf_directory'] = MODX_CORE_PATH . 'model/phpthumb/fonts/';
    $config['modphpthumb']['config_document_root'] = $modx->getOption('phpthumb_document_root', null, '');
}
$phpThumb = new phpthumb();
// unfortunately we have to create a new object for each image!
foreach ($config['modphpthumb'] as $param => $value) {
    // add MODX system settings
    $phpThumb->{$param} = $value;
}
foreach ($ptOptions as $param => $value) {
    // add options passed to the snippet
    $phpThumb->setParameter($param, $value);
}
// try to avert problems when $_SERVER['DOCUMENT_ROOT'] is different than MODX_BASE_PATH
if (!$phpThumb->config_document_root) {
    $phpThumb->config_document_root = MODX_BASE_PATH;
    // default if nothing set from system settings
}
$phpThumb->config_cache_directory = $assetsPath . 'cache/';
// doesn't matter, but saves phpThumb some frustration
$phpThumb->setSourceFilename($src);
/* setup cache filename that is unique to this tag */
$inputSanitized = str_replace(array(':', '/'), '_', $src);
$cacheFilename = $inputSanitized;
$cacheFilename .= '.' . md5(serialize($scriptProperties));
$cacheFilename .= '.' . (!empty($ptOptions['f']) ? $ptOptions['f'] : 'png');
$cacheKey = $assetsPath . 'cache/' . $cacheFilename;