3 namespace Depage\Graphics;
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/";
100 $imgUrl = substr($url, strlen($baseUrl) + 1);
103 $success = preg_match(
"/(.*\.(jpg|jpeg|gif|png|webp|eps|tif|tiff|pdf|svg))\.([^\\\]*)\.(jpg|jpeg|gif|png|webp)/i", $imgUrl, $matches);
106 $this->invalidAction =
true;
111 $this->
id = rawurldecode($matches[0]);
112 $this->srcImg = $relativePath . rawurldecode($matches[1]);
113 $this->outImg = $this->cachePath . rawurldecode($matches[0]);
121 $actions = explode(
".", $actionString);
124 $regex = implode(
"|", array_keys($this->aliases));
125 preg_match(
"/^($regex)/i", $action, $matches);
127 if (isset($matches[1]) && isset($this->aliases[$matches[1]])) {
128 $func = $this->aliases[$matches[1]];
129 $params = substr($action, strlen($matches[1]));
136 $params = preg_split(
"/[-x,]+/", $params,
null, PREG_SPLIT_NO_EMPTY);
138 if ($func ==
"addBackground") {
139 if (!in_array($params[0], array(
"transparent",
"checkerboard"))) {
140 $params[0] =
"#{$params[0]}";
143 foreach ($params as &$p) {
151 $this->actions[] = array($func, $params);
153 $this->invalidAction =
true;
162 $url = $_SERVER[
"REQUEST_URI"];
166 if ($this->invalidAction) {
170 $outDir = dirname($this->outImg);
171 if (!is_dir($outDir)) {
172 mkdir($outDir, 0755,
true);
174 if (file_exists($this->outImg) && filemtime($this->outImg) >= filemtime($this->srcImg)) {
183 foreach ($this->actions as $action) {
184 list($func, $params) = $action;
185 if (is_callable(array($graphics, $func))) {
186 call_user_func_array(array($graphics, $func), $params);
191 $graphics->render($this->srcImg, $this->outImg);
193 $this->rendered =
true;
194 }
catch (Exceptions\FileNotFound $e) {
195 $this->notFound =
true;
197 }
catch (Exceptions\Exception $e) {
198 $this->invalidAction =
true;
206 $info = pathinfo($this->outImg);
207 $ext = strtolower($info[
'extension']);
209 if ($this->invalidAction) {
210 header(
"HTTP/1.1 500 Internal Server Error");
211 echo(
"invalid image action");
214 if ($this->notFound) {
215 header(
"HTTP/1.1 404 Not Found");
216 echo(
"file not found");
220 if ($ext ==
"jpg" || $ext ==
"jpeg") {
221 header(
"Content-type: image/jpeg");
222 } elseif ($ext ==
"png") {
223 header(
"Content-type: image/png");
224 } elseif ($ext ==
"gif") {
225 header(
"Content-type: image/gif");
226 } elseif ($ext ==
"webp") {
227 header(
"Content-type: image/webp");
229 readfile($this->outImg);
236 $info = pathinfo($img);
237 $ext = $info[
'extension'];
239 if (count($this->actions) > 0) {
240 return $img .
"." . implode(
".", $this->actions) .
"." . $ext;
248 $this->actions[] =
"bg{$background}";
252 public function addCrop($width, $height, $x = 0, $y = 0)
254 $this->actions[] =
"crop{$width}x{$height}-{$x}x{$y}";
260 $this->actions[] =
"r{$width}x{$height}";
266 $this->actions[] =
"t{$width}x{$height}";
272 $this->actions[] =
"tf{$width}x{$height}";
278 $this->actions[] =
"q{$quality}";
static factory($options=array())
graphics object factory
addThumb($width, $height)
addThumbfill($width, $height)
__construct($options=array())
addCrop($width, $height, $x=0, $y=0)
analyzeActions($actionString)
addBackground($background)
addResize($width, $height)