public function thumbnail_img($params)
{
extract($params);
if (!isset($width)) {
$width = 200;
}
if (!isset($height)) {
$width = 200;
}
if (!isset($src) or $src == false) {
return $this->pixum($width, $height);
}
$src = strtok($src, '?');
$surl = $this->app->url_manager->site();
$local = false;
$media_url = media_base_url();
$media_url = trim($media_url);
$src = str_replace('{SITE_URL}', $surl, $src);
$src = str_replace('%7BSITE_URL%7D', $surl, $src);
$src = str_replace('..', '', $src);
if (strstr($src, $surl) or strpos($src, $surl)) {
$src = str_replace($surl . '/', $surl, $src);
//$src = str_replace($media_url, '', $src);
$src = str_replace($surl, '', $src);
$src = ltrim($src, DS);
$src = ltrim($src, '/');
$src = rtrim($src, DS);
$src = rtrim($src, '/');
//$src = media_base_path() . $src;
$src = MW_ROOTPATH . $src;
$src = normalize_path($src, false);
} else {
$src1 = media_base_path() . $src;
$src1 = normalize_path($src1, false);
$src2 = MW_ROOTPATH . $src;
$src2 = normalize_path($src2, false);
$src3 = strtolower($src2);
if (is_file($src1)) {
$src = $src1;
} elseif (is_file($src2)) {
$src = $src2;
} elseif (is_file($src3)) {
$src = $src3;
} else {
$no_img = true;
if ($no_img) {
return $this->pixum_img();
}
}
}
$media_root = media_base_path();
$cd = $this->thumbnails_path() . $width . DS;
if (!is_dir($cd)) {
mkdir_recursive($cd);
}
$index_file = $cd . 'index.html';
if (!is_file($index_file)) {
file_put_contents($index_file, 'Thumbnail directory is not allowed');
}
if (!isset($ext)) {
$ext = strtolower(get_file_extension($src));
}
$cache = md5(serialize($params)) . '.' . $ext;
$cache = str_replace(' ', '_', $cache);
if (isset($cache_id)) {
$cache = str_replace(' ', '_', $cache_id);
$cache = str_replace('..', '', $cache);
}
$cache_path = $cd . $cache;
if (file_exists($cache_path)) {
if (!headers_sent()) {
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
$if_modified_since = '';
}
$mtime = filemtime($src);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ($if_modified_since == $gmdate_mod) {
header('HTTP/1.0 304 Not Modified');
}
}
} else {
if (file_exists($src)) {
if ($ext == 'svg') {
$res1 = file_get_contents($src);
$res1 = $this->svgScaleHack($res1, $width, $height);
file_put_contents($cache_path, $res1);
} else {
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png' || $ext == 'bmp') {
$tn = new \Microweber\Utils\Thumbnailer($src);
$thumbOptions = array('maxLength' => $height, 'width' => $width);
$tn->createThumb($thumbOptions, $cache_path);
unset($tn);
} else {
return $this->pixum_img();
}
}
}
}
$ext = get_file_extension($cache_path);
if ($ext == 'jpg') {
$ext = 'jpeg';
}
header('Content-Type: image/' . $ext);
header('Content-Length: ' . filesize($cache_path));
readfile($cache_path);
exit;
}