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); } } }
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; }
function render($image, $params) { //Set defaults $path = ''; $width = 150; $height = 225; $quality = 75; //Extract Parameters if (isset($params['path'])) { $path = $params['path'] . DS; } if (isset($params['width'])) { $width = $params['width']; } if (isset($params['height'])) { $height = $params['height']; } if (isset($params['quality'])) { $quality = $params['quality']; } //import phpThumb class app::import('Vendor', 'phpthumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php')); $thumbNail = new phpthumb(); $thumbNail->src = WWW_ROOT . 'img' . DS . $path . $image; $thumbNail->w = $width; $thumbNail->h = $height; $thumbNail->q = $quality; $thumbNail->config_imagemagick_path = '/usr/bin/convert'; $thumbNail->config_prefer_imagemagick = true; $thumbNail->config_output_format = 'jpg'; $thumbNail->config_error_die_on_error = true; $thumbNail->config_document_root = ''; $thumbNail->config_temp_directory = APP . 'tmp'; $thumbNail->config_cache_directory = WWW_ROOT . 'img' . DS . 'thumbs' . DS; $thumbNail->config_cache_disable_warning = true; $cacheFilename = $image; $thumbNail->cache_filename = $thumbNail->config_cache_directory . $cacheFilename; if (!is_file($thumbNail->cache_filename)) { if ($thumbNail->GenerateThumbnail()) { $thumbNail->RenderToFile($thumbNail->cache_filename); } } if (is_file($thumbNail->cache_filename)) { return $cacheFilename; } }
/** * 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); } } }
public static function small($file) { require_once dirname(__FILE__) . '/../phpthumb/phpthumb.class.php'; $basefile = new File($file); $basepath = File::a2r($file); $webimg = dirname($basepath) . "/" . $basefile->name . "_small." . $basefile->extension; list($x, $y) = getimagesize($file); if ($x <= 1200 && $y <= 1200) { return $file; } $path = File::r2a($webimg, Settings::$thumbs_dir); /// Create smaller image if (!file_exists($path) || filectime($file) > filectime($path)) { if (!file_exists(dirname($path))) { @mkdir(dirname($path), 0755, true); } $thumb = new phpthumb(); $thumb->config_imagemagick_path = Settings::$imagemagick_path; $thumb->setSourceData(file_get_contents($file)); $thumb->CalculateThumbnailDimensions(); $thumb->w = 1200; $thumb->h = 1200; $thumb->q = Settings::$quality_small; if (File::Type($file) == 'Image' && Provider::get_orientation_degrees($file) != 0) { $thumb->SourceImageToGD(); //$thumb->ra = Provider::get_orientation_degrees($file); $thumb->Rotate(); } $thumb->GenerateThumbnail(); $thumb->RenderToFile($path); } return $path; }
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; } }
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)) { mkdir($mk); chmod($mk, 0777); } $path .= "/" . $dir; } } $folder .= "/"; } $outputFilename = $_SERVER['DOCUMENT_ROOT'] . "/assets/cache/images/" . $folder . $filename . "." . $ext; if ($cache && file_exists($outputFilename)) { unlink($outputFilename); } if (!file_exists($outputFilename)) { if ($phpThumb->GenerateThumbnail()) { $phpThumb->RenderToFile($outputFilename); } } $res = explode("/assets", $outputFilename); $res = "/assets" . $res[1]; return $res;
/** * 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); } } }
/** * 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; } }
/** * 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); } } }
/** * 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; }
} //////////////////////////////////////////////////////////////// // Debug output, to try and help me diagnose problems if (@$_GET['phpthumbDebug'] == '7') { $phpthumb->phpthumbDebug(); } //////////////////////////////////////////////////////////////// $phpthumb->GenerateThumbnail(); //////////////////////////////////////////////////////////////// // Debug output, to try and help me diagnose problems if (@$_GET['phpthumbDebug'] == '8') { $phpthumb->phpthumbDebug(); } //////////////////////////////////////////////////////////////// if ($phpthumb->file) { $phpthumb->RenderToFile($phpthumb->ResolveFilenameToAbsolute($phpthumb->file)); if ($phpthumb->goto && substr(strtolower($phpthumb->goto), 0, strlen('http://')) == 'http://') { // redirect to another URL after image has been rendered to file header('Location: ' . $phpthumb->goto); exit; } } else { if (file_exists($phpthumb->cache_filename) && is_writable($phpthumb->cache_filename) || is_writable(dirname($phpthumb->cache_filename))) { $phpthumb->CleanUpCacheDirectory(); $phpthumb->RenderToFile($phpthumb->cache_filename); } else { $phpthumb->DebugMessage('Cannot write to $phpthumb->cache_filename (' . $phpthumb->cache_filename . ') because that directory (' . dirname($phpthumb->cache_filename) . ') is not writable', __FILE__, __LINE__); } } //////////////////////////////////////////////////////////////// // Debug output, to try and help me diagnose problems
$totalTime = sprintf("%2.4f s", $totalTime); $modx->log(modX::LOG_LEVEL_DEBUG, "\n<br />Execution time: {$totalTime}\n<br />"); $modx->setLogLevel($oldLogLevel); $modx->setLogTarget($oldLogTarget); } $output = $assetsUrl; /* check to see if there's a cached file of this already */ if (file_exists($cacheKey)) { $modx->log(modX::LOG_LEVEL_DEBUG, '[phpThumbOf] Using cached file found for thumb: ' . $cacheKey); $output = str_replace(' ', '%20', $cacheUrl); } else { /* actually make the thumbnail */ //return $cacheKey; if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it! if ($phpThumb->RenderToFile($cacheKey)) { $output = str_replace(' ', '%20', $cacheUrl); } else { $modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbOf] Could not cache thumb "' . $src . '" to file at: ' . $cacheKey . ' - Debug: ' . print_r($phpThumb->debugmessages, true)); } } else { $modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbOf] Could not generate thumbnail: ' . $src . ' - Debug: ' . print_r($phpThumb->debugmessages, true)); } } if (!headers_sent()) { $headers = $modx->request->getHeaders(); $mtime = filemtime($cacheKey); if (isset($headers['If-Modified-Since']) && strtotime($headers['If-Modified-Since']) == $mtime) { // cache is good, send 304 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT', true, 304); exit;