/** * Generate a thumbnail with a random name for an image. * * @access public * @param array $fileInfo An array of file information. * @return string html file list to prefill the template */ public function generateThumbnail($fileInfo = array()) { if (file_exists($fileInfo['path'] . $fileInfo['uniqueName'])) { if (!isset($fileInfo['thumbName'])) { $path_info = pathinfo($fileInfo['uniqueName']); $thumbOptions = array(); if (in_array(strtolower($path_info['extension']), array('jpg', 'jpeg', 'png', 'gif'))) { $thumbOptions['src'] = $fileInfo['path'] . $fileInfo['uniqueName']; if ($this->config['thumbX']) { $thumbOptions['w'] = $this->config['thumbX']; } if ($this->config['thumbY']) { $thumbOptions['h'] = $this->config['thumbY']; } if ($this->config['thumbX'] && $this->config['thumbY']) { $thumbOptions['zc'] = '1'; } } else { $thumbOptions['src'] = $this->config['assetsPath'] . '/images/generic.png'; $thumbOptions['aoe'] = '1'; $thumbOptions['fltr'] = array('wmt|' . strtoupper($path_info['extension']) . '|5|C|000000'); if ($this->config['thumbX']) { $thumbOptions['w'] = $this->config['thumbX']; } if ($this->config['thumbY']) { $thumbOptions['h'] = $this->config['thumbY']; } if ($this->config['thumbX'] && $this->config['thumbY']) { $thumbOptions['zc'] = '1'; } $thumbOptions['f'] = 'png'; $path_info['extension'] = 'png'; } $thumbName = md5($path_info['basename'] . time() . '.thumb') . '.' . $path_info['extension']; // generate Thumbnail & save it $phpThumb = new modPhpThumb($this->modx, $thumbOptions); $phpThumb->initialize(); if ($phpThumb->GenerateThumbnail()) { if (!$phpThumb->RenderToFile($fileInfo['path'] . $thumbName)) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Thumbnail generation: Thumbnail not saved.' . "\nDebugmessages:\n" . implode("\n", $phpThumb->debugmessages), '', 'AjaxUpload'); $this->debug[] = 'Thumbnail generation: Thumbnail not saved.' . "\nDebugmessaes:\n" . implode("\n", $phpThumb->debugmessages); } else { $filePerm = (int) $this->config['newFilePermissions']; if (!@chmod($fileInfo['path'] . $thumbName, octdec($filePerm))) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not change the thumbnail file permission.', '', 'AjaxUpload'); } } } else { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Thumbnail generation: Thumbnail not created.' . "\nDebugmessages:\n" . implode("\n", $phpThumb->debugmessages), '', 'AjaxUpload'); $this->debug[] = 'Thumbnail generation: Thumbnail not created.' . "\nDebugmessaes:\n" . implode("\n", $phpThumb->debugmessages); } $fileInfo['thumbName'] = $thumbName; } return $fileInfo['thumbName']; } else { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Thumbnail generation: Original file not found.', '', 'AjaxUpload'); $this->debug[] = 'Thumbnail generation: Original file not found'; return false; } }
/** * Creates the thumbnail file provided if it doesn't already exist based on the $options array. * * @param string $file absolute path to the thumbnail * @param array $options key/value options passed to modPhpThumb * @return bool true if thumb already exists or gets created, false if there is an error */ protected function create_thumb($file, $options) { if ($this->check_if_exists($file, $options['src'])) { return true; } if (!$this->check_cache_dir()) { return false; } if (!$this->modx->loadClass('modPhpThumb', $this->modx->getOption('core_path') . 'model/phpthumb/', true, true)) { $this->modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbsUp] Could not load modPhpThumb class.'); return false; } // NOTE: there's a bug in modPhpThumb that doesn't generate the path correctly when in the manager // context, so we have to manually prepend a slash to the src path if in the mgr context if ($this->modx->context->key == 'mgr') { $options['src'] = '/' . $options['src']; } $pt = new modPhpThumb($this->modx); $pt->config = array_merge($pt->config, $options); $pt->initialize(); $pt->GenerateThumbnail(); $pt->RenderToFile($file); return true; }
/** * Render the thumbnail * @return mixed|string */ public function render() { $this->getCacheFilename(); $this->getCacheUrl(); $this->cleanCache(); $this->phpThumbOf->startDebug(); /* if using s3, check for file there */ $useS3 = $this->modx->getOption('useS3', $this->config, false); if ($useS3) { $expired = $this->checkForS3Cache(); if ($expired !== true) { return $expired; } } $this->checkCacheFilePermissions(); $this->phpThumbOf->endDebug(); if ($cacheUrl = $this->checkForCachedFile()) { return $cacheUrl; } /* actually make the thumbnail */ if ($this->phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it! if ($this->phpThumb->RenderToFile($this->cacheKey)) { $this->checkCacheFilePermissions(); if ($useS3) { $this->cacheUrl = $this->pushToS3(); } return str_replace(' ', '%20', $this->cacheUrl); } else { $this->modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbOf] Could not cache thumb "' . $this->input . '" to file at: ' . $this->cacheKey . ' - Debug: ' . print_r($this->phpThumb->debugmessages, true)); } } else { $this->modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbOf] Could not generate thumbnail: ' . $this->input . ' - Debug: ' . print_r($this->phpThumb->debugmessages, true)); } return ''; }