27 'quality' =>
"setQuality",
31 'resize' =>
"addResize",
33 'thumbfill' =>
"addThumbfill",
34 'tf' =>
"addThumbfill",
35 'thumb' =>
"addThumb",
37 'background' =>
"addBackground",
38 'bg' =>
"addBackground",
54 $this->invalidAction =
false;
55 $this->notFound =
false;
56 $this->rendered =
false;
60 if (isset($this->options[
'baseUrl']) && isset($this->options[
'cachePath'])) {
61 $baseUrl = rtrim($this->options[
'baseUrl'],
'/');
62 $this->cachePath = $this->options[
'cachePath'];
63 $relativePath = $this->options[
'relPath'] ??
'';
64 }
else if (defined(
'DEPAGE_PATH') && defined(
'DEPAGE_CACHE_PATH')) {
66 $info = parse_url(DEPAGE_BASE);
67 $baseUrl = rtrim($info[
'path'],
'/');
69 $this->cachePath = \DEPAGE_CACHE_PATH .
"graphics/";
72 $scriptParts = explode(
"/", $_SERVER[
"SCRIPT_NAME"]);
73 $uriParts = explode(
"/", $_SERVER[
"REQUEST_URI"]);
75 if (strpos($_SERVER[
"SCRIPT_NAME"],
"/lib/") !==
false) {
76 for ($i = 0; $i < count($uriParts); $i++) {
78 if ($uriParts[$i] ==
"lib") {
83 for ($i = 0; $i < count($uriParts); $i++) {
85 if ($scriptParts[$i] != $uriParts[$i]) {
90 $baseUrl = implode(
"/", array_slice($uriParts, 0, $i));
91 if (isset($this->options[
'relPath'])) {
92 $relativePath = $this->options[
'relPath'];
94 $relativePath = str_repeat(
"../", count($scriptParts) - $i - 1);
96 $this->cachePath = $relativePath .
"lib/cache/graphics/";
98 $baseUrlStatic = $baseUrl;
99 if (isset($this->options[
'baseUrlStatic'])) {
100 $baseUrlStatic = rtrim($this->options[
'baseUrlStatic'],
'/');
105 if (strpos($url, $baseUrlStatic) === 0) {
106 $imgUrl = substr($url, strlen($baseUrlStatic) + 1);
107 }
else if (strpos($url, $baseUrl) === 0) {
108 $imgUrl = substr($url, strlen($baseUrl) + 1);
112 $success = preg_match(
"/(.*\.(jpg|jpeg|gif|png|webp|eps|tif|tiff|pdf|svg))\.([^\\\]*)\.(jpg|jpeg|gif|png|webp)/i", $imgUrl, $matches);
115 $this->invalidAction =
true;
120 $this->
id = rawurldecode($matches[0]);
121 $this->srcImg = $relativePath . rawurldecode($matches[1]);
122 $this->outImg = $this->cachePath . rawurldecode($matches[0]);
130 $actions = explode(
".", $actionString);
133 $regex = implode(
"|", array_keys($this->aliases));
134 preg_match(
"/^($regex)/i", $action, $matches);
136 if (isset($matches[1]) && isset($this->aliases[$matches[1]])) {
137 $func = $this->aliases[$matches[1]];
138 $params = substr($action, strlen($matches[1]));
145 $params = preg_split(
"/[-x,]+/", $params,
null, PREG_SPLIT_NO_EMPTY);
147 if ($func ==
"addBackground") {
148 if (!in_array($params[0], array(
"transparent",
"checkerboard"))) {
149 $params[0] =
"#{$params[0]}";
152 foreach ($params as $i => &$p) {
154 if ($p == 0 && $i < 2) {
160 $this->actions[] = array($func, $params);
162 $this->invalidAction =
true;
171 $url = $_SERVER[
"REQUEST_URI"];
175 if ($this->invalidAction) {
179 $outDir = dirname($this->outImg);
180 if (!is_dir($outDir)) {
181 mkdir($outDir, 0755,
true);
183 if (file_exists($this->outImg) && filemtime($this->outImg) >= filemtime($this->srcImg)) {
192 foreach ($this->actions as $action) {
193 list($func, $params) = $action;
194 if (is_callable(array($graphics, $func))) {
195 call_user_func_array(array($graphics, $func), $params);
200 $graphics->render($this->srcImg, $this->outImg);
202 $this->rendered =
true;
203 }
catch (Exceptions\FileNotFound $e) {
204 $this->notFound =
true;
206 }
catch (Exceptions\Exception $e) {
207 $this->invalidAction =
true;
215 $info = pathinfo($this->outImg);
216 $ext = strtolower($info[
'extension']);
218 if ($this->invalidAction) {
219 header(
"HTTP/1.1 500 Internal Server Error");
220 echo(
"invalid image action");
223 if ($this->notFound) {
224 header(
"HTTP/1.1 404 Not Found");
225 echo(
"file not found");
229 if ($ext ==
"jpg" || $ext ==
"jpeg") {
230 header(
"Content-type: image/jpeg");
231 } elseif ($ext ==
"png") {
232 header(
"Content-type: image/png");
233 } elseif ($ext ==
"gif") {
234 header(
"Content-type: image/gif");
235 } elseif ($ext ==
"webp") {
236 header(
"Content-type: image/webp");
238 readfile($this->outImg);
245 $info = pathinfo($img);
246 $ext = $info[
'extension'];
248 if (count($this->actions) > 0) {
249 return $img .
"." . implode(
".", $this->actions) .
"." . $ext;
257 $this->actions[] =
"bg{$background}";
261 public function addCrop($width, $height, $x = 0, $y = 0)
263 $this->actions[] =
"crop{$width}x{$height}-{$x}x{$y}";
269 $this->actions[] =
"r{$width}x{$height}";
275 $this->actions[] =
"t{$width}x{$height}";
279 public function addThumbfill($width, $height, $centerX = 50, $centerY = 50)
281 $action =
"tf{$width}x{$height}";
282 if ($centerX != 50 || $centerY != 50) {
283 $action .=
"-{$centerX}x{$centerY}";
286 $this->actions[] = $action;
292 $this->actions[] =
"q{$quality}";
static factory($options=array())
graphics object factory
addThumbfill($width, $height, $centerX=50, $centerY=50)
addThumb($width, $height)
__construct($options=array())
addCrop($width, $height, $x=0, $y=0)
analyzeActions($actionString)
addBackground($background)
addResize($width, $height)