37 if ($this->imgAct && is_resource($this->imgAct))
38 @imagedestroy($this->imgAct);
39 if ($this->imgTmp && is_resource($this->imgTmp))
40 @imagedestroy($this->imgTmp);
51 public function upload($file, array $prm = array()) {
52 $this->cfg->file = $file;
53 if (!array_key_exists(
'fileSaveAdd', $prm))
54 $this->cfg->fileSave = $file;
55 $this->cfg->setA($prm);
56 $this->cfg->rebuild =
true;
57 return basename($this->
build());
67 public function view($file, array $prm = array()) {
69 $this->cfg->setA($prm);
70 $this->cfg->html =
true;
71 if ($this->cfg->originalView) {
72 $this->cfg->fileSave = $this->cfg->file;
75 $this->cfg->fileSave = $this->
makePath($this->cfg->file, $this->cfg->fileSaveAdd);
76 $ret = $this->
build();
78 $fileWeb = str_replace($this->cfg->filesRoot,
'', $this->cfg->fileSave);
92 public function delete($file, array $prm=null) {
106 $more = md5($this->cfg->w.
'_'.$this->cfg->h.
'_'.$this->cfg->bgColor.
'_'.$this->cfg->fit);
110 if (!$this->cfg->forceExt)
114 '/\.('.implode(
'|', $this->cfg->autoExt).
')$/i',
115 $more.
'.'.$this->cfg->forceExt,
126 if (strpos($file, $this->cfg->filesRoot) ===
false)
127 $file = $this->cfg->filesRoot.$file;
138 public function make(array $prm = array()) {
139 $this->cfg->setA($prm);
140 return $this->
build();
143 public function valid(array $file, array $prm = array()) {
144 $ret = parent::valid($file, $prm);
145 if ($ret && count($file) && array_key_exists(
'tmp_name', $file) &&
file::exists($file[
'tmp_name'])) {
146 $size = getimagesize($file[
'tmp_name']);
147 if (!is_array($size) && $size[2] != IMAGETYPE_GIF && $size[2] != IMAGETYPE_JPEG && $size[2] != IMAGETYPE_PNG)
148 return $this->cfg->getInArray(
'validErrors',
'notValidImg');
161 $this->cfg->wAct = null;
162 $this->cfg->hAct = null;
164 if ($this->cfg->autoFileSave && empty($this->cfg->fileSave))
165 $this->cfg->fileSave = $this->
makePath($this->cfg->file, $this->cfg->fileSaveAdd);
167 if ($this->cfg->rebuild || !
file::exists($this->cfg->fileSave)) {
168 $this->
setImg($this->cfg->file);
170 if (is_array($this->cfg->crop)) {
171 if ($this->
crop($this->cfg->crop)) {
173 $this->cfg->wAct = imagesx($this->imgAct);
174 $this->cfg->hAct = imagesy($this->imgAct);
179 if ($this->cfg->resizeSmaller ||
180 ($this->cfg->w > 0 && $this->cfg->wAct > $this->cfg->w) ||
181 ($this->cfg->h > 0 && $this->cfg->hAct > $this->cfg->h)) {
185 'bgColor'=>$this->cfg->bgColor,
186 'fit'=>$this->cfg->fit,
187 'useMaxResize'=>$this->cfg->useMaxResize
190 $this->cfg->wAct = imagesx($this->imgAct);
191 $this->cfg->hAct = imagesy($this->imgAct);
192 if ($this->cfg->w || $this->cfg->h)
198 if (!empty($this->cfg->filters) && function_exists(
'imagefilter')) {
199 foreach($this->cfg->filters as $prms) {
200 if (!is_array($prms)) {
204 $f = array_shift($prms);
205 switch(count($prms)) {
206 case 0: imagefilter($this->imgAct,
$f);
break;
207 case 1: imagefilter($this->imgAct,
$f, $prms[0]);
break;
208 case 2: imagefilter($this->imgAct,
$f, $prms[0], $prms[1]);
break;
209 case 3: imagefilter($this->imgAct,
$f, $prms[0], $prms[1], $prms[2]);
break;
210 default: imagefilter($this->imgAct,
$f, $prms[0], $prms[1], $prms[2], $prms[3]);
break;
215 if ($this->cfg->grayFilter) {
217 if (function_exists(
'imagefilter')) {
218 imagefilter($this->imgAct, IMG_FILTER_GRAYSCALE);
224 $this->imgAct = imagecreatetruecolor($x, $y);
225 imagecolorallocate($this->imgAct, 0, 0, 0);
226 for ($i = 0; $i < $x; $i++) {
227 for ($j = 0; $j < $y; $j++) {
228 $rgb = imagecolorat(
$imgTmp, $i, $j);
229 $r = ($rgb >> 16) & 0xFF;
230 $g = ($rgb >> 8) & 0xFF;
232 $color = max(array($r, $g, $b));
233 imagesetpixel($this->imgAct, $i, $j, imagecolorexact($this->imgAct, $color, $color, $color));
240 if (!empty($this->cfg->mask) &&
file::exists($this->cfg->mask)) {
241 $this->
mask($this->cfg->mask);
245 if (!empty($this->cfg->watermarks) && is_array($this->cfg->watermarks)) {
246 foreach($this->cfg->watermarks as $watermark) {
248 $change = $change || $tmp;
254 $this->cfg->fileSave = $this->cfg->file;
256 if (!empty($this->cfg->fileSave)) {
258 if ($this->
save($this->cfg->fileSave))
259 $ret = $this->cfg->fileSave;
261 if ($this->cfg->file != $this->cfg->fileSave)
262 file::move($this->cfg->file, $this->cfg->fileSave);
263 $ret = $this->cfg->fileSave;
267 if ($this->cfg->html)
268 $ret = $this->
html();
269 }
else if ($this->cfg->html) {
270 $ret = $this->
html();
272 $ret = $this->cfg->fileSave;
284 protected function html(array $options = array()) {
286 $w = $this->cfg->wAct ? $this->cfg->wAct : null;
287 $h = $this->cfg->hAct ? $this->cfg->hAct : null;
288 if ($this->cfg->forceHtmlSize && (!$w || !$h)) {
289 $size = getimagesize($this->cfg->fileSave);
296 array_merge($options, $this->cfg->htmlDefOptions, array(
297 'src'=>$this->cfg->fileSave,
298 'alt'=>$this->cfg->alt,
313 $this->cfg->img = $file;
317 $this->imgAct = $tmp[0];
318 $this->cfg->wAct = $tmp[1];
319 $this->cfg->hAct = $tmp[2];
333 switch ($this->cfg->forceExt) {
335 $ret = imagegif($this->imgAct, $file);
338 $ret = imagepng($this->imgAct, $file);
341 $ret = imagejpeg($this->imgAct, $file, $this->cfg->jpgQuality);
345 $this->cfg->fileSave = $file;
358 $this->info = getimagesize($file);
361 switch ($this->info[2]) {
363 $img = imagecreatefromjpeg($file);
366 $img = imagecreatefromgif($file);
367 imagealphablending($img,
false);
368 imagesavealpha($img,
true);
371 $img = imagecreatefrompng($file);
372 imagealphablending($img,
false);
373 imagesavealpha($img,
true);
379 return array(&$img, $this->info[0], $this->info[1]);
394 protected function resize(array $prm = array()) {
400 'useMaxResize'=>
false,
404 $img = &$this->{
'img'.$prm[
'imgName']};
405 $srcW = $this->cfg->get(
'w'.$prm[
'imgName']);
406 $srcH = $this->cfg->get(
'h'.$prm[
'imgName']);
412 $scaleW = $prm[
'w'] / $srcW;
413 $scaleH = $prm[
'h'] / $srcH;
417 if ($prm[
'useMaxResize'] && $prm[
'w'] && $prm[
'h']) {
418 if ($scaleW > $scaleH) {
425 if ($prm[
'w'] && $prm[
'h']) {
428 if ($scaleW > $scaleH) {
429 $srcH = round($prm[
'h'] / $scaleW);
430 $srcY = round(($this->cfg->get(
'h'.$prm[
'imgName']) - $srcH) / 2);
432 $srcW = round($prm[
'w'] / $scaleH);
433 $srcX = round(($this->cfg->get(
'w'.$prm[
'imgName']) - $srcW) / 2);
436 if ($scaleW > $scaleH) {
437 $dstW = round($srcW * $scaleH);
438 $dstX = round(($prm[
'w'] - $dstW) / 2);
440 $dstH = round($srcH * $scaleW);
441 $dstY = round(($prm[
'h'] - $dstH) / 2);
444 }
else if ($prm[
'w']) {
446 $prm[
'h'] = round($srcH * $scaleW);
447 $dstH = round($srcH * $scaleW);
449 }
else if ($prm[
'h']) {
451 $prm[
'w'] = round($srcW * $scaleH);
452 $dstW = round($srcW * $scaleH);
456 $prm[
'w'] = $this->cfg->wAct;
457 $prm[
'h'] = $this->cfg->hAct;
462 $imgDst = imagecreatetruecolor($prm[
'w'], $prm[
'h']);
463 if ($this->info[2] == IMAGETYPE_GIF || $this->info[2] == IMAGETYPE_PNG) {
464 imagealphablending($imgDst,
false);
465 imagesavealpha($imgDst,
true);
468 if (empty($prm[
'bgColor']) && ($this->cfg->forceExt ==
'png' || $this->cfg->forceExt ==
'gif' || $this->info[2] == IMAGETYPE_GIF || $this->info[2] == IMAGETYPE_PNG)) {
469 $transparency = imagecolortransparent($img);
470 if ($transparency >= 0) {
471 $trnprtIndex = imagecolortransparent($img);
472 $transparentColor = imagecolorsforindex($img, $trnprtIndex);
473 $transparency = imagecolorallocate($imgDst, $transparentColor[
'red'], $transparentColor[
'green'], $transparentColor[
'blue']);
474 imagefill($imgDst, 0, 0, $transparency);
475 imagecolortransparent($imgDst, $transparency);
476 }
else if ($this->info[2] == IMAGETYPE_PNG) {
477 imagealphablending($imgDst,
false);
478 imagesavealpha($imgDst,
true);
479 imagefill($imgDst, 0, 0, imagecolorallocatealpha($imgDst, 0, 0, 0, 127));
481 }
else if (!$prm[
'fit']) {
482 $cl = $this->
hexa2dec($prm[
'bgColor']);
483 $clR = imagecolorallocate($imgDst, $cl[0], $cl[1], $cl[2]);
484 imagefill($imgDst, 0, 0, $clR);
488 imagecopyresampled($imgDst, $img, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
494 $this->{
'img'.$prm[
'imgName']} = &$imgDst;
508 $this->imgTmp = $tmp[0];
509 $this->cfg->wTmp = $tmp[1];
510 $this->cfg->hTmp = $tmp[2];
518 $this->cfg->wTmp = imagesx($this->imgTmp);
519 $this->cfg->hTmp = imagesy($this->imgTmp);
522 $newPicture = imagecreatetruecolor($this->cfg->wTmp, $this->cfg->hTmp);
523 imagealphablending($newPicture,
false);
524 imagesavealpha($newPicture,
true);
525 imagefill($newPicture, 0, 0, imagecolorallocatealpha($newPicture, 0, 0, 0, 127));
528 for( $x = 0; $x < $this->cfg->wTmp; $x++ ) {
529 for( $y = 0; $y < $this->cfg->hTmp; $y++ ) {
530 $alpha = imagecolorsforindex($this->imgTmp, imagecolorat($this->imgTmp, $x, $y));
531 $alpha = 127 - floor($alpha[
'red' ] / 2);
532 $color = imagecolorsforindex($this->imgAct, imagecolorat($this->imgAct, $x, $y));
533 imagesetpixel($newPicture, $x, $y, imagecolorallocatealpha($newPicture, $color[
'red'], $color[
'green'], $color[
'blue'], $alpha));
537 imagedestroy($this->imgAct);
538 $this->imgAct = $newPicture;
562 $this->imgTmp = $tmp[0];
563 $this->cfg->wTmp = $tmp[1];
564 $this->cfg->hTmp = $tmp[2];
565 if (strpos($prm[
'margin'],
'%') !==
false) {
566 $val = intval(substr($prm[
'margin'], 0, -1)) / 100;
567 $marginW = $val * $this->cfg->wAct;
568 $marginH = $val * $this->cfg->hAct;
570 $marginW = $marginH = $prm[
'margin'];
576 $srcW = $this->cfg->wTmp;
577 $srcH = $this->cfg->hTmp;
578 $dstW = $this->cfg->wAct - ($marginW * 2);
579 $dstH = $this->cfg->hAct - ($marginH * 2);
581 if (!$prm[
'fit'] && $prm[
'center']) {
582 $scaleW = $dstW / $srcW;
583 $scaleH = $dstH / $srcH;
584 if ($scaleW > $scaleH) {
585 $dstW = round($srcW * $scaleH);
586 $dstX = round(($this->cfg->wAct - $dstW) / 2);
588 $dstH = round($srcH * $scaleW);
589 $dstY = round(($this->cfg->hAct - $dstH) / 2);
601 imagecopyresampled($this->imgAct, $this->imgTmp, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
618 protected function crop(array $prm) {
635 $w = $this->cfg->h * $h / $this->cfg->w;
637 $h = $this->cfg->w * $w / $this->cfg->h;
641 $x = round($this->cfg->wAct / 2 - $w / 2);
644 $y = round($this->cfg->hAct / 2 - $h / 2);
646 $this->imgTmp = imagecreatetruecolor($this->cfg->w, $this->cfg->h);
647 imagecopyresampled($this->imgTmp, $this->imgAct, 0, 0, $x, $y, $this->cfg->w, $this->cfg->h, $w, $h);
662 base_convert(substr($col, 0, 2), 16, 10),
663 base_convert(substr($col, 2, 2), 16, 10),
664 base_convert(substr($col, 4, 2), 16, 10)
makePath($file, $more=null)
static htmlTag($tag, array $attributes, $content=null)
addFilesRootIfNeeded($file)
static move($oldName, $newName)
static initTab(array &$vars, array $init)
resize(array $prm=array())
static webUri($uri, $absolute=false)
html(array $options=array())
static uploadedUri($file, array $prm=array())
view($file, array $prm=array())
upload($file, array $prm=array())
valid(array $file, array $prm=array())
static multipleDelete($pattern, $matchPattern=null)