* A custom output filter for phpThumb
 *
 * @var modX $modx
 * @var array $scriptProperties
 * @var phpThumbOf $phpThumbOf
 * @var string $input
 * @var string|array $options
 *
 * @package phpthumbof
 */
if (empty($modx)) {
    return '';
}
if (!$modx->loadClass('modPhpThumb', $modx->getOption('core_path') . 'model/phpthumb/', true, true)) {
    $modx->log(modX::LOG_LEVEL_ERROR, '[phpThumbOf] Could not load modPhpThumb class.');
    return '';
}
if (empty($input)) {
    $modx->log(modX::LOG_LEVEL_DEBUG, '[phpThumbOf] Empty image path passed, aborting.');
    return '';
}
$modelPath = $modx->getOption('phpthumbof.core_path', null, $modx->getOption('core_path') . 'components/phpthumbof/') . 'model/';
require_once $modelPath . 'phpthumbof/phpthumbof.class.php';
$phpThumbOf = new phpThumbOf($modx, $scriptProperties);
$phpThumbOf->getCacheDirectory();
$phpThumbOf->ensureCacheDirectoryIsWritable();
$thumbnail = $phpThumbOf->createThumbnailObject();
$thumbnail->setInput($input);
$thumbnail->setOptions($options);
$thumbnail->initializeService();
return $thumbnail->render();
Example #2
0
 * @var string|array $options
 *
 */
if (empty($input)) {
    // Exit quietly if no file name given
    return;
}
$scriptProperties['debug'] = isset($debug) ? $debug : false;
static $pt_settings = array();
if (empty($pt_settings)) {
    if (!$modx->loadClass('phpThumbOf', MODX_CORE_PATH . 'components/phpthumbof/model/', true, true)) {
        $modx->log(modX::LOG_LEVEL_ERROR, '[pThumb] Could not load phpThumbOf class.');
        return $input;
    }
}
$pThumb = new phpThumbOf($modx, $pt_settings, $scriptProperties);
$result = $pThumb->createThumbnail($input, $options);
if (!empty($toPlaceholder) || $result['outputDims']) {
    if ($result['width'] === '' && $result['file'] && ($dims = getimagesize($result['file']))) {
        $result['width'] = $dims[0];
        $result['height'] = $dims[1];
    }
    if (!empty($toPlaceholder)) {
        $modx->setPlaceholders(array($toPlaceholder => $result['src'], "{$toPlaceholder}.width" => $result['width'], "{$toPlaceholder}.height" => $result['height']));
        $output = '';
    }
    if ($result['outputDims']) {
        $output = "src=\"{$result['src']}\"" . ($result['width'] ? " width=\"{$result['width']}\" height=\"{$result['height']}\"" : '');
    }
} else {
    $output = $result['src'];
 /**
  * See if the file is cached on S3.
  * @return mixed
  */
 public function checkForS3Cache()
 {
     /* if using a CNAME alias, set here (ensure is postfixed with /) */
     $s3hostAliasLen = strlen($this->config['s3hostAlias']);
     if (!empty($this->config['s3hostAlias'])) {
         $this->config['s3hostAlias'] = str_replace(array('http://', 'https://'), '', $this->config['s3hostAlias']);
         if (substr($this->config['s3hostAlias'], $s3hostAliasLen - 1, $s3hostAliasLen) != '/') {
             $this->config['s3hostAlias'] .= '/';
         }
     }
     $s3host = !empty($this->config['s3hostAlias']) ? $this->config['s3hostAlias'] : $this->config['s3hostDefault'];
     /* calc relative path of image in s3 bucket */
     $path = str_replace('//', '/', $this->config['s3path'] . $this->cacheFilename);
     $this->expired = true;
     $lastModified = 0;
     $s3imageUrl = '';
     /* check with php's get_headers (slower) */
     if ($this->config['s3headersCheck']) {
         $this->modx->log(modX::LOG_LEVEL_DEBUG, '[phpthumbof] Using get_headers to check modified.');
         $s3imageUrl = 'http://' . str_replace('//', '/', $s3host . urlencode($path));
         $headers = get_headers($s3imageUrl, 1);
         if (!empty($headers) && !empty($headers[0]) && $headers[0] == 'HTTP/1.1 200 OK') {
             if (empty($headers['Last-Modified'])) {
                 $this->expired = true;
             } else {
                 $this->expired = false;
                 $lastModified = $headers['Last-Modified'];
                 $lastModified = strtotime(trim($lastModified[1]));
             }
         } else {
             $this->expired = true;
         }
     } else {
         /* otherwise use amazon's (faster) get object info */
         $this->modx->log(modX::LOG_LEVEL_DEBUG, '[phpthumbof] Using get_object_url to check modified.');
         $s3response = $this->aws->getFileUrl($path);
         if (!empty($s3response) && is_object($s3response) && !empty($s3response->body) && !empty($s3response->status) && $s3response->status == 200) {
             /* check expiry for image */
             $this->expired = false;
             $lastModified = strtotime($s3response->header['last-modified']);
             $s3imageUrl = $s3response->header['_info']['url'];
             if (!empty($this->config['s3hostAlias'])) {
                 $s3imageUrl = str_replace($this->config['s3hostDefault'], $this->config['s3hostAlias'], $s3imageUrl);
             }
         }
     }
     /* check to see if expired */
     if (!empty($lastModified)) {
         /* use last-modified to determine age */
         $maxAge = (int) $this->modx->getOption('phpthumbof.s3_cache_time', null, 24) * 60 * 60;
         $now = time();
         if ($now - $lastModified > $maxAge) {
             $this->expired = true;
         }
     }
     /* if not expired past the cache time, use that url. otherwise, delete from S3 */
     if (!$this->expired) {
         $this->phpThumbOf->endDebug();
         return $s3imageUrl;
     }
     $this->aws->deleteObject($path);
     return true;
 }