00001 <?php
00010 class helper_image extends helper_file {
00011
00017 protected $imgAct;
00018
00024 protected $imgTmp;
00025
00031 protected $info;
00032
00036 public function __destruct() {
00037 if ($this->imgAct && is_resource($this->imgAct))
00038 @imagedestroy($this->imgAct);
00039 if ($this->imgTmp && is_resource($this->imgTmp))
00040 @imagedestroy($this->imgTmp);
00041 }
00042
00051 public function upload($file, array $prm = array()) {
00052 $this->cfg->file = $file;
00053 if (!array_key_exists('fileSaveAdd', $prm))
00054 $this->cfg->fileSave = $file;
00055 $this->cfg->setA($prm);
00056 $this->cfg->rebuild = true;
00057 return basename($this->build());
00058 }
00059
00067 public function view($file, array $prm = array()) {
00068 $this->cfg->file = $this->addFilesRootIfNeeded($file);
00069 $this->cfg->setA($prm);
00070 $this->cfg->html = true;
00071 if ($this->cfg->originalView) {
00072 $this->cfg->fileSave = $this->cfg->file;
00073 $ret = $this->html();
00074 } else {
00075 $this->cfg->fileSave = $this->makePath($this->cfg->file, $this->cfg->fileSaveAdd);
00076 $ret = $this->build();
00077 }
00078 $fileWeb = str_replace($this->cfg->filesRoot, '', $this->cfg->fileSave);
00079 return str_replace(
00080 $this->cfg->fileSave,
00081 $this->cfg->webUri ? request::webUri($fileWeb) : request::uploadedUri($fileWeb),
00082 $ret);
00083 }
00084
00092 public function delete($file, array $prm=null) {
00093 file::delete($this->addFilesRootIfNeeded($file));
00094 file::multipleDelete($this->addFilesRootIfNeeded($this->makePath($file, '*')));
00095 }
00096
00104 public function makePath($file, $more=null) {
00105 if (is_null($more))
00106 $more = md5($this->cfg->w.'_'.$this->cfg->h.'_'.$this->cfg->bgColor.'_'.$this->cfg->fit);
00107 if ($more)
00108 $more = '_'.$more;
00109
00110 return preg_replace(
00111 '/\.('.implode('|', $this->cfg->autoExt).')$/i',
00112 $more.'.'.file::getExt($file),
00113 $file);
00114 }
00115
00122 protected function addFilesRootIfNeeded($file) {
00123 if (strpos($file, $this->cfg->filesRoot) === false)
00124 $file = $this->cfg->filesRoot.$file;
00125 return $file;
00126 }
00127
00135 public function make(array $prm = array()) {
00136 $this->cfg->setA($prm);
00137 return $this->build();
00138 }
00139
00140 public function valid(array $file, array $prm = array()) {
00141 $ret = parent::valid($file, $prm);
00142 if ($ret && count($file) && array_key_exists('tmp_name', $file) && file::exists($file['tmp_name'])) {
00143 $size = getimagesize($file['tmp_name']);
00144 if (!is_array($size) && $size[2] != IMAGETYPE_GIF && $size[2] != IMAGETYPE_JPEG && $size[2] != IMAGETYPE_PNG)
00145 return 'notValidImg';
00146 }
00147 return $ret;
00148 }
00149
00155 protected function build() {
00156 $ret = null;
00157 if (file::exists($this->cfg->file)) {
00158
00159 if ($this->cfg->autoFileSave && empty($this->cfg->fileSave))
00160 $this->cfg->fileSave = $this->makePath($this->cfg->file, $this->cfg->fileSaveAdd);
00161
00162 if ($this->cfg->rebuild || !file::exists($this->cfg->fileSave)) {
00163 $this->setImg($this->cfg->file);
00164 $change = false;
00165 if (is_array($this->cfg->crop)) {
00166 if ($this->crop($this->cfg->crop)) {
00167
00168 $this->cfg->wAct = imagesx($this->imgAct);
00169 $this->cfg->hAct = imagesy($this->imgAct);
00170 $change = true;
00171 }
00172 } else {
00173
00174 if ($this->cfg->resizeSmaller ||
00175 ($this->cfg->w > 0 && $this->cfg->wAct > $this->cfg->w) ||
00176 ($this->cfg->h > 0 && $this->cfg->hAct > $this->cfg->h)) {
00177 if ($this->resize(array(
00178 'w'=>$this->cfg->w,
00179 'h'=>$this->cfg->h,
00180 'bgColor'=>$this->cfg->bgColor,
00181 'fit'=>$this->cfg->fit
00182 ))) {
00183
00184 $this->cfg->wAct = imagesx($this->imgAct);
00185 $this->cfg->hAct = imagesy($this->imgAct);
00186 if ($this->cfg->w || $this->cfg->h)
00187 $change = true;
00188 }
00189 }
00190 }
00191
00192 if (!empty($this->cfg->filters) && function_exists('imagefilter')) {
00193 foreach($this->cfg->filters as $prms) {
00194 if (!is_array($prms)) {
00195 $f = $prms;
00196 $prms = array();
00197 } else
00198 $f = array_shift($prms);
00199 switch(count($prms)) {
00200 case 0: imagefilter($this->imgAct, $f); break;
00201 case 1: imagefilter($this->imgAct, $f, $prms[0]); break;
00202 case 2: imagefilter($this->imgAct, $f, $prms[0], $prms[1]); break;
00203 case 3: imagefilter($this->imgAct, $f, $prms[0], $prms[1], $prms[2]); break;
00204 default: imagefilter($this->imgAct, $f, $prms[0], $prms[1], $prms[2], $prms[3]); break;
00205 }
00206 }
00207 }
00208
00209 if ($this->cfg->grayFilter) {
00210
00211 if (function_exists('imagefilter')) {
00212 imagefilter($this->imgAct, IMG_FILTER_GRAYSCALE);
00213 } else {
00214
00215 $imgTmp = $this->imgAct;
00216 $x = imagesx($imgTmp);
00217 $y = imagesy($imgTmp);
00218 $this->imgAct = imagecreatetruecolor($x, $y);
00219 imagecolorallocate($this->imgAct, 0, 0, 0);
00220 for ($i = 0; $i < $x; $i++) {
00221 for ($j = 0; $j < $y; $j++) {
00222 $rgb = imagecolorat($imgTmp, $i, $j);
00223 $r = ($rgb >> 16) & 0xFF;
00224 $g = ($rgb >> 8) & 0xFF;
00225 $b = $rgb & 0xFF;
00226 $color = max(array($r, $g, $b));
00227 imagesetpixel($this->imgAct, $i, $j, imagecolorexact($this->imgAct, $color, $color, $color));
00228 }
00229 }
00230 imagedestroy($imgTmp);
00231 }
00232 }
00233
00234 if (!empty($this->cfg->mask) && file::exists($this->cfg->mask)) {
00235 $this->mask($this->cfg->mask);
00236 $change = true;
00237 }
00238
00239 $ret = null;
00240 if (!$change)
00241 $this->cfg->fileSave = $this->cfg->file;
00242
00243 if (!empty($this->cfg->fileSave)) {
00244 if ($change) {
00245 if ($this->save($this->cfg->fileSave))
00246 $ret = $this->cfg->fileSave;
00247 } else {
00248 if ($this->cfg->file != $this->cfg->fileSave)
00249 file::move($this->cfg->file, $this->cfg->fileSave);
00250 $ret = $this->cfg->fileSave;
00251 }
00252 }
00253
00254 if ($this->cfg->html)
00255 $ret = $this->html();
00256 } else if ($this->cfg->html) {
00257 $ret = $this->html();
00258 } else {
00259 $ret = $this->cfg->fileSave;
00260 }
00261 }
00262
00263 return $ret;
00264 }
00265
00271 protected function html(array $options = array()) {
00272 if (file::exists($this->cfg->fileSave)) {
00273 return utils::htmlTag('img',
00274 array_merge($options, $this->cfg->htmlDefOptions, array(
00275 'src'=>$this->cfg->fileSave,
00276 'alt'=>$this->cfg->alt,
00277 'width'=>$this->cfg->w ? $this->cfg->w : null,
00278 'height'=>$this->cfg->h ? $this->cfg->h : null,
00279 )));
00280 }
00281 return null;
00282 }
00283
00290 public function setImg($file) {
00291 $this->cfg->img = $file;
00292
00293 $tmp = $this->createImage($file);
00294 if ($tmp) {
00295 $this->imgAct = $tmp[0];
00296 $this->cfg->wAct = $tmp[1];
00297 $this->cfg->hAct = $tmp[2];
00298 return true;
00299 } else
00300 return false;
00301 }
00302
00308 public function save($file) {
00309 $ret = false;
00310
00311 switch (strtolower(file::getExt($file))) {
00312 case 'gif' :
00313 $ret = imagegif($this->imgAct, $file);
00314 break;
00315 case 'png' :
00316 $ret = imagepng($this->imgAct, $file);
00317 break;
00318 default:
00319 $ret = imagejpeg($this->imgAct, $file, $this->cfg->jpgQuality);
00320 break;
00321 }
00322 if ($ret)
00323 $this->cfg->fileSave = $file;
00324 return $ret;
00325 }
00326
00333 protected function createImage($file) {
00334 if (!file::exists($file) || ! is_file($file))
00335 return false;
00336 $this->info = getimagesize($file);
00337
00338 $img = null;
00339 switch ($this->info[2]) {
00340 case IMAGETYPE_JPEG:
00341 $img = imagecreatefromjpeg($file);
00342 break;
00343 case IMAGETYPE_GIF:
00344 $img = imagecreatefromgif($file);
00345 imagealphablending($img, true);
00346 imagesavealpha($img, true);
00347 break;
00348 case IMAGETYPE_PNG:
00349 $img = imagecreatefrompng($file);
00350 imagealphablending($img, true);
00351 imagesavealpha($img, true);
00352 break;
00353 default:
00354 return false;
00355 }
00356
00357 return array(&$img, $this->info[0], $this->info[1]);
00358 }
00359
00371 protected function resize(array $prm = array()) {
00372 config::initTab($prm, array(
00373 'imgName'=>'Act',
00374 'w'=>0,
00375 'h'=>0,
00376 'fit'=>false,
00377 'bgColor'=>'ffffff'
00378 ));
00379
00380 $img = &$this->{'img'.$prm['imgName']};
00381 $srcW = $this->cfg->get('w'.$prm['imgName']);
00382 $srcH = $this->cfg->get('h'.$prm['imgName']);
00383
00384 $srcX = 0;
00385 $srcY = 0;
00386 $dstX = 0;
00387 $dstY = 0;
00388 $scaleW = $prm['w'] / $srcW;
00389 $scaleH = $prm['h'] / $srcH;
00390 $dstW = $prm['w'];
00391 $dstH = $prm['h'];
00392
00393 if ($prm['w'] && $prm['h']) {
00394
00395 if ($prm['fit']) {
00396 if ($scaleW > $scaleH) {
00397 $srcH = round($prm['h'] / $scaleW);
00398 $srcY = round(($this->cfg->get('h'.$prm['imgName']) - $srcH) / 2);
00399 } else {
00400 $srcW = round($prm['w'] / $scaleH);
00401 $srcX = round(($this->cfg->get('w'.$prm['imgName']) - $srcW) / 2);
00402 }
00403 } else {
00404 if ($scaleW > $scaleH) {
00405 $dstW = round($srcW * $scaleH);
00406 $dstX = round(($prm['w'] - $dstW) / 2);
00407 } else {
00408 $dstH = round($srcH * $scaleW);
00409 $dstY = round(($prm['h'] - $dstH) / 2);
00410 }
00411 }
00412 } else if ($prm['w']) {
00413
00414 $prm['h'] = round($srcH * $scaleW);
00415 $dstH = round($srcH * $scaleW);
00416 $prm['fit'] = true;
00417 } else if ($prm['h']) {
00418
00419 $prm['w'] = round($srcW * $scaleH);
00420 $dstW = round($srcW * $scaleH);
00421 $prm['fit'] = true;
00422 } else {
00423
00424 $prm['w'] = $this->cfg->wAct;
00425 $prm['h'] = $this->cfg->hAct;
00426 $dstW = $prm['w'];
00427 $dstH = $prm['h'];
00428 }
00429
00430 $imgDst = imagecreatetruecolor($prm['w'], $prm['h']);
00431 if ($this->info[2] == IMAGETYPE_GIF || $this->info[2] == IMAGETYPE_PNG) {
00432 imagealphablending($imgDst, true);
00433 imagesavealpha($imgDst, true);
00434 }
00435
00436 if (empty($prm['bgColor']) && ($this->info[2] == IMAGETYPE_GIF || $this->info[2] == IMAGETYPE_PNG)) {
00437 $transparency = imagecolortransparent($img);
00438 if ($transparency >= 0) {
00439 $trnprtIndex = imagecolortransparent($img);
00440 $transparentColor = imagecolorsforindex($img, $trnprtIndex);
00441 $transparency = imagecolorallocate($imgDst, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
00442 imagefill($imgDst, 0, 0, $transparency);
00443 imagecolortransparent($imgDst, $transparency);
00444 } else if ($this->info[2] == IMAGETYPE_PNG) {
00445 imagealphablending($imgDst, false);
00446 imagesavealpha($imgDst, true);
00447 $color = imagecolorallocatealpha($imgDst, 255, 255, 255, 127);
00448 imagefilledrectangle($imgDst, 0, 0, $prm['w'], $prm['h'], $color);
00449 }
00450 } else if (!$prm['fit']) {
00451 $cl = $this->hexa2dec($prm['bgColor']);
00452 $clR = imagecolorallocate($imgDst, $cl[0], $cl[1], $cl[2]);
00453 imagefill($imgDst, 0, 0, $clR);
00454 }
00455
00456
00457 imagecopyresampled($imgDst, $img, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
00458
00459
00460 imagedestroy($img);
00461
00462
00463 $this->{'img'.$prm['imgName']} = &$imgDst;
00464
00465 return true;
00466 }
00467
00473 public function mask($file) {
00474
00475 $tmp = $this->createImage($file);
00476 if ($tmp) {
00477 $this->imgTmp = $tmp[0];
00478 $this->cfg->wTmp = $tmp[1];
00479 $this->cfg->hTmp = $tmp[2];
00480
00481
00482 if ($this->resize(array(
00483 'imgName'=>'Tmp',
00484 'bgColor'=>false
00485 ))) {
00486
00487 $this->cfg->wTmp = imagesx($this->imgTmp);
00488 $this->cfg->hTmp = imagesy($this->imgTmp);
00489 }
00490
00491 imagecopymerge($this->imgAct, $this->imgTmp, 0, 0, 0, 0, $this->cfg->wAct, $this->cfg->hAct, 100);
00492 }
00493 }
00494
00505 protected function crop(array $prm) {
00506 config::initTab($prm, array(
00507 'x'=>-1,
00508 'y'=>-1,
00509 'w'=>0,
00510 'h'=>0
00511 ));
00512 $x = $prm['x'];
00513 $y = $prm['y'];
00514 $w = $prm['w'];
00515 $h = $prm['h'];
00516
00517 if ($w + $h == 0) {
00518 $w = $this->cfg->w;
00519 $h = $this->cfg->h;
00520 } else {
00521 if ($w == 0)
00522 $w = $this->cfg->h * $h / $this->cfg->w;
00523 if ($h == 0)
00524 $h = $this->cfg->w * $w / $this->cfg->h;
00525 }
00526
00527 if ($x == -1)
00528 $x = round($this->cfg->wAct / 2 - $w / 2);
00529
00530 if ($y == -1)
00531 $y = round($this->cfg->hAct / 2 - $h / 2);
00532
00533 $this->imgTmp = imagecreatetruecolor($this->cfg->w, $this->cfg->h);
00534 imagecopyresampled($this->imgTmp, $this->imgAct, 0, 0, $x, $y, $this->cfg->w, $this->cfg->h, $w, $h);
00535
00536 $this->imgAct = $this->imgTmp;
00537
00538 return true;
00539 }
00540
00547 protected function hexa2dec($col) {
00548 return array(
00549 base_convert(substr($col, 0, 2), 16, 10),
00550 base_convert(substr($col, 2, 2), 16, 10),
00551 base_convert(substr($col, 4, 2), 16, 10)
00552 );
00553 }
00554
00555 }