nyroFwk  0.2
image.class.php
Go to the documentation of this file.
1 <?php
10 class helper_image extends helper_file {
11 
17  protected $imgAct;
18 
24  protected $imgTmp;
25 
31  protected $info;
32 
36  public function __destruct() {
37  if ($this->imgAct && is_resource($this->imgAct))
38  @imagedestroy($this->imgAct);
39  if ($this->imgTmp && is_resource($this->imgTmp))
40  @imagedestroy($this->imgTmp);
41  }
42 
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());
58  }
59 
67  public function view($file, array $prm = array()) {
68  $this->cfg->file = $this->addFilesRootIfNeeded($file);
69  $this->cfg->setA($prm);
70  $this->cfg->html = true;
71  if ($this->cfg->originalView) {
72  $this->cfg->fileSave = $this->cfg->file;
73  $ret = $this->html();
74  } else {
75  $this->cfg->fileSave = $this->makePath($this->cfg->file, $this->cfg->fileSaveAdd);
76  $ret = $this->build();
77  }
78  $fileWeb = str_replace($this->cfg->filesRoot, '', $this->cfg->fileSave);
79  return str_replace(
80  $this->cfg->fileSave,
81  $this->cfg->webUri ? request::webUri($fileWeb) : request::uploadedUri($fileWeb),
82  $ret);
83  }
84 
92  public function delete($file, array $prm=null) {
93  file::delete($this->addFilesRootIfNeeded($file));
94  file::multipleDelete($this->addFilesRootIfNeeded($this->makePath($file, '*')), '@'.str_replace('\\', '\\\\', $this->addFilesRootIfNeeded($this->makePath($file, '([^_]*)'))).'$@i');
95  }
96 
104  public function makePath($file, $more=null) {
105  if (is_null($more))
106  $more = md5($this->cfg->w.'_'.$this->cfg->h.'_'.$this->cfg->bgColor.'_'.$this->cfg->fit);
107  if ($more)
108  $more = '_'.$more;
109 
110  if (!$this->cfg->forceExt)
111  $this->cfg->forceExt = file::getExt($file);
112 
113  return preg_replace(
114  '/\.('.implode('|', $this->cfg->autoExt).')$/i',
115  $more.'.'.$this->cfg->forceExt,
116  $file);
117  }
118 
125  protected function addFilesRootIfNeeded($file) {
126  if (strpos($file, $this->cfg->filesRoot) === false)
127  $file = $this->cfg->filesRoot.$file;
128  return $file;
129  }
130 
138  public function make(array $prm = array()) {
139  $this->cfg->setA($prm);
140  return $this->build();
141  }
142 
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');
149  }
150  return $ret;
151  }
152 
158  protected function build() {
159  $ret = null;
160  if (file::exists($this->cfg->file)) {
161  $this->cfg->wAct = null;
162  $this->cfg->hAct = null;
163 
164  if ($this->cfg->autoFileSave && empty($this->cfg->fileSave))
165  $this->cfg->fileSave = $this->makePath($this->cfg->file, $this->cfg->fileSaveAdd);
166 
167  if ($this->cfg->rebuild || !file::exists($this->cfg->fileSave)) {
168  $this->setImg($this->cfg->file);
169  $change = false;
170  if (is_array($this->cfg->crop)) {
171  if ($this->crop($this->cfg->crop)) {
172  // Save the new size
173  $this->cfg->wAct = imagesx($this->imgAct);
174  $this->cfg->hAct = imagesy($this->imgAct);
175  $change = true;
176  }
177  } else {
178  // Resize
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)) {
182  if ($this->resize(array(
183  'w'=>$this->cfg->w,
184  'h'=>$this->cfg->h,
185  'bgColor'=>$this->cfg->bgColor,
186  'fit'=>$this->cfg->fit,
187  'useMaxResize'=>$this->cfg->useMaxResize
188  ))) {
189  // Save the new size
190  $this->cfg->wAct = imagesx($this->imgAct);
191  $this->cfg->hAct = imagesy($this->imgAct);
192  if ($this->cfg->w || $this->cfg->h)
193  $change = true;
194  }
195  }
196  }
197 
198  if (!empty($this->cfg->filters) && function_exists('imagefilter')) {
199  foreach($this->cfg->filters as $prms) {
200  if (!is_array($prms)) {
201  $f = $prms;
202  $prms = array();
203  } else
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;
211  }
212  }
213  }
214 
215  if ($this->cfg->grayFilter) {
216  // Copy form $img to $imgDst With the parameter defined, with grayscale
217  if (function_exists('imagefilter')) {
218  imagefilter($this->imgAct, IMG_FILTER_GRAYSCALE);
219  } else {
220  // Manual Grayscale
222  $x = imagesx($imgTmp);
223  $y = imagesy($imgTmp);
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;
231  $b = $rgb & 0xFF;
232  $color = max(array($r, $g, $b));
233  imagesetpixel($this->imgAct, $i, $j, imagecolorexact($this->imgAct, $color, $color, $color));
234  }
235  }
236  imagedestroy($imgTmp);
237  }
238  }
239 
240  if (!empty($this->cfg->mask) && file::exists($this->cfg->mask)) {
241  $this->mask($this->cfg->mask);
242  $change = true;
243  }
244 
245  if (!empty($this->cfg->watermarks) && is_array($this->cfg->watermarks)) {
246  foreach($this->cfg->watermarks as $watermark) {
247  $tmp = $this->watermark($watermark);
248  $change = $change || $tmp;
249  }
250  }
251 
252  $ret = null;
253  if (!$change)
254  $this->cfg->fileSave = $this->cfg->file;
255 
256  if (!empty($this->cfg->fileSave)) {
257  if ($change) {
258  if ($this->save($this->cfg->fileSave))
259  $ret = $this->cfg->fileSave;
260  } else {
261  if ($this->cfg->file != $this->cfg->fileSave)
262  file::move($this->cfg->file, $this->cfg->fileSave);
263  $ret = $this->cfg->fileSave;
264  }
265  }
266 
267  if ($this->cfg->html)
268  $ret = $this->html();
269  } else if ($this->cfg->html) {
270  $ret = $this->html();
271  } else {
272  $ret = $this->cfg->fileSave;
273  }
274  }
275 
276  return $ret;
277  }
278 
284  protected function html(array $options = array()) {
285  if (file::exists($this->cfg->fileSave)) {
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);
290  if (!$w)
291  $w = $size[0];
292  if (!$h)
293  $h = $size[1];
294  }
295  return utils::htmlTag('img',
296  array_merge($options, $this->cfg->htmlDefOptions, array(
297  'src'=>$this->cfg->fileSave,
298  'alt'=>$this->cfg->alt,
299  'width'=>$w,
300  'height'=>$h,
301  )));
302  }
303  return null;
304  }
305 
312  public function setImg($file) {
313  $this->cfg->img = $file;
314 
315  $tmp = $this->createImage($file);
316  if ($tmp) {
317  $this->imgAct = $tmp[0];
318  $this->cfg->wAct = $tmp[1];
319  $this->cfg->hAct = $tmp[2];
320  return true;
321  } else
322  return false;
323  }
324 
330  public function save($file) {
331  $ret = false;
332 
333  switch ($this->cfg->forceExt) {
334  case 'gif' :
335  $ret = imagegif($this->imgAct, $file);
336  break;
337  case 'png' :
338  $ret = imagepng($this->imgAct, $file);
339  break;
340  default:
341  $ret = imagejpeg($this->imgAct, $file, $this->cfg->jpgQuality);
342  break;
343  }
344  if ($ret)
345  $this->cfg->fileSave = $file;
346  return $ret;
347  }
348 
355  protected function createImage($file) {
356  if (!file::exists($file) || ! is_file($file))
357  return false;
358  $this->info = getimagesize($file);
359 
360  $img = null;
361  switch ($this->info[2]) {
362  case IMAGETYPE_JPEG:
363  $img = imagecreatefromjpeg($file);
364  break;
365  case IMAGETYPE_GIF:
366  $img = imagecreatefromgif($file);
367  imagealphablending($img, false);
368  imagesavealpha($img, true);
369  break;
370  case IMAGETYPE_PNG:
371  $img = imagecreatefrompng($file);
372  imagealphablending($img, false);
373  imagesavealpha($img, true);
374  break;
375  default:
376  return false;
377  }
378 
379  return array(&$img, $this->info[0], $this->info[1]);
380  }
381 
394  protected function resize(array $prm = array()) {
395  config::initTab($prm, array(
396  'imgName'=>'Act',
397  'w'=>0,
398  'h'=>0,
399  'fit'=>false,
400  'useMaxResize'=>false,
401  'bgColor'=>'ffffff'
402  ));
403 
404  $img = &$this->{'img'.$prm['imgName']};
405  $srcW = $this->cfg->get('w'.$prm['imgName']);
406  $srcH = $this->cfg->get('h'.$prm['imgName']);
407 
408  $srcX = 0;
409  $srcY = 0;
410  $dstX = 0;
411  $dstY = 0;
412  $scaleW = $prm['w'] / $srcW;
413  $scaleH = $prm['h'] / $srcH;
414  $dstW = $prm['w'];
415  $dstH = $prm['h'];
416 
417  if ($prm['useMaxResize'] && $prm['w'] && $prm['h']) {
418  if ($scaleW > $scaleH) {
419  $prm['w'] = 0;
420  } else {
421  $prm['h'] = 0;
422  }
423  }
424 
425  if ($prm['w'] && $prm['h']) {
426  // Dimensions are fixed
427  if ($prm['fit']) {
428  if ($scaleW > $scaleH) {
429  $srcH = round($prm['h'] / $scaleW);
430  $srcY = round(($this->cfg->get('h'.$prm['imgName']) - $srcH) / 2);
431  } else {
432  $srcW = round($prm['w'] / $scaleH);
433  $srcX = round(($this->cfg->get('w'.$prm['imgName']) - $srcW) / 2);
434  }
435  } else {
436  if ($scaleW > $scaleH) {
437  $dstW = round($srcW * $scaleH);
438  $dstX = round(($prm['w'] - $dstW) / 2);
439  } else {
440  $dstH = round($srcH * $scaleW);
441  $dstY = round(($prm['h'] - $dstH) / 2);
442  }
443  }
444  } else if ($prm['w']) {
445  // Width is fixed
446  $prm['h'] = round($srcH * $scaleW);
447  $dstH = round($srcH * $scaleW);
448  $prm['fit'] = true;
449  } else if ($prm['h']) {
450  // Height is fixed
451  $prm['w'] = round($srcW * $scaleH);
452  $dstW = round($srcW * $scaleH);
453  $prm['fit'] = true;
454  } else {
455  // No dimensions requested, use the imgAct dimensions
456  $prm['w'] = $this->cfg->wAct;
457  $prm['h'] = $this->cfg->hAct;
458  $dstW = $prm['w'];
459  $dstH = $prm['h'];
460  }
461 
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);
466  }
467 
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));
480  }
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);
485  }
486 
487  // Copy form $img to $imgDst With the parameter defined
488  imagecopyresampled($imgDst, $img, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
489 
490  // Destroy the image ressource source
491  imagedestroy($img);
492 
493  // Store the new
494  $this->{'img'.$prm['imgName']} = &$imgDst;
495 
496  return true;
497  }
498 
504  public function mask($file) {
505  // Create the mask image ressource
506  $tmp = $this->createImage($file);
507  if ($tmp) {
508  $this->imgTmp = $tmp[0];
509  $this->cfg->wTmp = $tmp[1];
510  $this->cfg->hTmp = $tmp[2];
511 
512  // Resize the mask
513  if ($this->resize(array(
514  'imgName'=>'Tmp',
515  'bgColor'=>false
516  ))) {
517  // Save the new size
518  $this->cfg->wTmp = imagesx($this->imgTmp);
519  $this->cfg->hTmp = imagesy($this->imgTmp);
520  }
521 
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));
526 
527  // Perform pixel-based alpha map application
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));
534  }
535  }
536 
537  imagedestroy($this->imgAct);
538  $this->imgAct = $newPicture;
539  }
540  }
541 
551  public function watermark($prm) {
552  $ret = false;
553  if (config::initTab($prm, array(
554  'file'=>null,
555  'fit'=>false,
556  'center'=>true,
557  'margin'=>0
558  ))) {
559  // Create the mask image ressource
560  $tmp = $this->createImage($prm['file']);
561  if ($tmp) {
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;
569  } else {
570  $marginW = $marginH = $prm['margin'];
571  }
572 
573  $dstX = $marginW;
574  $dstY = $marginH;
575  $srcX = $srcY = 0;
576  $srcW = $this->cfg->wTmp;
577  $srcH = $this->cfg->hTmp;
578  $dstW = $this->cfg->wAct - ($marginW * 2);
579  $dstH = $this->cfg->hAct - ($marginH * 2);
580 
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);
587  } else {
588  $dstH = round($srcH * $scaleW);
589  $dstY = round(($this->cfg->hAct - $dstH) / 2);
590  }
591  /*
592  if ($scaleW > $scaleH) {
593  $dstW = round($srcW * $scaleH);
594  $dstX = round(($srcW - $dstW) / 2);
595  } else {
596  $dstH = round($srcH * $scaleW);
597  $dstY = round(($srcH - $dstH) / 2);
598  }
599  */
600  }
601  imagecopyresampled($this->imgAct, $this->imgTmp, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
602  $ret = true;
603  }
604  }
605  return $ret;
606  }
607 
618  protected function crop(array $prm) {
619  config::initTab($prm, array(
620  'x'=>-1,
621  'y'=>-1,
622  'w'=>0,
623  'h'=>0
624  ));
625  $x = $prm['x'];
626  $y = $prm['y'];
627  $w = $prm['w'];
628  $h = $prm['h'];
629 
630  if ($w + $h == 0) {
631  $w = $this->cfg->w;
632  $h = $this->cfg->h;
633  } else {
634  if ($w == 0)
635  $w = $this->cfg->h * $h / $this->cfg->w;
636  if ($h == 0)
637  $h = $this->cfg->w * $w / $this->cfg->h;
638  }
639 
640  if ($x == -1)
641  $x = round($this->cfg->wAct / 2 - $w / 2);
642 
643  if ($y == -1)
644  $y = round($this->cfg->hAct / 2 - $h / 2);
645 
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);
648 
649  $this->imgAct = $this->imgTmp;
650 
651  return true;
652  }
653 
660  protected function hexa2dec($col) {
661  return array(
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)
665  );
666  }
667 
668 }
makePath($file, $more=null)
make(array $prm=array())
static htmlTag($tag, array $attributes, $content=null)
Definition: utils.class.php:46
addFilesRootIfNeeded($file)
static exists($file)
Definition: file.class.php:97
static move($oldName, $newName)
Definition: file.class.php:279
static initTab(array &$vars, array $init)
crop(array $prm)
resize(array $prm=array())
static getExt($file)
Definition: file.class.php:400
static webUri($uri, $absolute=false)
html(array $options=array())
static uploadedUri($file, array $prm=array())
view($file, array $prm=array())
Definition: image.class.php:67
upload($file, array $prm=array())
Definition: image.class.php:51
static delete($file)
Definition: file.class.php:355
valid(array $file, array $prm=array())
static multipleDelete($pattern, $matchPattern=null)
Definition: file.class.php:386
$f
Definition: list.php:6
createImage($file)
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13