1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19: 20:
21:
22: class Image_Image
23: {
24:
25: const IMG_GIF = 1;
26: const IMG_JPG = 2;
27: const IMG_PNG = 3;
28: const IMG_SWF = 4;
29: const IMG_PSD = 5;
30: const IMG_BMP = 6;
31: const IMG_TIF = 7;
32: 33: 34: 35:
36: protected $width;
37:
38: 39: 40: 41:
42: protected $height;
43:
44: 45: 46: 47: 48: 49:
50: protected $type;
51:
52: 53: 54: 55:
56: protected $mime;
57: 58: 59: 60:
61: protected $source;
62:
63: 64: 65: 66:
67: protected $font;
68:
69: 70: 71: 72:
73: protected $couleur;
74:
75: 76: 77: 78:
79: protected $infoImage;
80:
81: 82: 83: 84:
85: protected $poids;
86:
87: private $copie;
88:
89: 90: 91: 92:
93: private $logo;
94: 95: 96: 97:
98: private $widthL;
99: 100: 101: 102:
103: private $heightL;
104: 105: 106: 107:
108: private $typeL;
109:
110:
111: 112: 113: 114: 115: 116:
117: public function __construct($img,$w=null,$h=null)
118: {
119: if(!empty($img) && file_exists($img))
120: {
121: if(!is_readable($img))
122: {
123: throw new Image_Exception ('Le fichier n\'est pas ouvert en lecture');
124: exit;
125: }
126: $info = getimagesize($img);
127: $this->width = $info[0];
128: $this->height = $info[1];
129: $this->type = $info[2];
130: $this->mime = $info['mime'];
131: $this->copie = null;
132: $this->source = $this->createFromType($img);
133: if($this->source == false)
134: {
135: throw new Image_Exception ('Format d\'image non supporté. Seul les formats JPG,PNG ou GIF sont admis');
136: exit;
137: }
138: $this->couleur = imagecolorallocate($this->source,187,3,33);
139: $this->poids = filesize($img);
140: $this->infoImage = array();
141: $this->infoImage['extension'] = strchr($img, '.');
142: $this->infoImage['extension'] = substr($this->infoImage['extension'], 1);
143: $this->infoImage['extension'] = strtolower($this->infoImage['extension']);
144: }
145: else
146: {
147: 148: 149: 150:
151: if($w>0 && $h>0)
152: {
153: $width = intval($w);
154: $height = intval($h);
155: $this->source = imagecreatetruecolor($width,$height);
156: $this->width = $width;
157: $this->height = $height;
158: $this->type = null;
159: }
160: else
161: {
162: throw new Image_Exception ('Si aucune image source n\'est fournie la taille est obligatoire');
163: exit;
164: }
165:
166: }
167: }
168:
169: 170: 171: 172: 173:
174: public function getInfo()
175: {
176: $this->infoImage['width'] = $this->width;
177: $this->infoImage['height'] = $this->height;
178: $this->infoImage['mime'] = $this->mime;
179: $this->infoImage['poids'] = round($this->poids/1024,2);
180:
181: return $this->infoImage;
182:
183: }
184: 185: 186: 187: 188:
189: public function addLogo($logo)
190: {
191: if(!file_exists($logo))
192: throw new Image_Exception('Le logo ne semble pas exister');
193:
194: $this->logo = new Image_Image($logo);
195:
196: $tailleLogo = $this->logo->getInfo();
197: $this->widthL = $tailleLogo['width'];
198: $this->heightL = $tailleLogo['height'];
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function mergeLogo($pos='bd',$opacite=75)
209: {
210: if($pos =='hg')
211: {
212: $posX = 0;
213: $posY = 0;
214: }
215: elseif($pos=='hd')
216: {
217: $posX = ($this->width - $this->widthL);
218: $posY = 0;
219: }
220: elseif($pos == 'bg')
221: {
222: $posX = 0;
223: $posY = ($this->height - $this->heightL);
224: }
225: elseif($pos == 'bd')
226: {
227: $posX = ($this->width - $this->widthL);
228: $posY = ($this->height - $this->heightL);
229: }
230: elseif($pos == 'ct')
231: {
232: $posX = (($this->width/2) - ($this->widthL/2));
233: $posY = (($this->height/2) - ($this->heightL/2));
234: }
235:
236: if(imagecopymerge($this->source,$this->logo->getSource(),$posX,$posY,0,0,$this->widthL,$this->heightL,$opacite))
237: return true;
238: else
239: return false;
240: }
241:
242: 243: 244: 245: 246: 247:
248: protected function createFromType($img)
249: {
250: if($this->type==self::IMG_GIF)
251: {
252: $crea = imagecreatefromgif($img);
253: imagealphablending($crea,TRUE);
254: }
255: elseif($this->type == self::IMG_JPG)
256: {
257: $crea = imagecreatefromjpeg($img);
258: }
259: elseif($this->type == self::IMG_PNG)
260: {
261: $crea = imagecreatefrompng($img);
262: imagealphablending($crea,true);
263: imagesavealpha($crea,true);
264: }
265: else
266: $crea = false;
267:
268: return $crea;
269: }
270:
271:
272: 273: 274: 275:
276: public function duplicate()
277: {
278: $this->copie = imagecreatetruecolor($this->width,$this->height);
279: imagecopy($this->copie,$this->source,0,0,0,0,$this->width,$this->height);
280: }
281:
282: 283: 284: 285:
286: public function restore()
287: {
288: $this->source = $this->copie;
289: }
290:
291: 292: 293: 294: 295:
296: public function setFont($path)
297: {
298: $font = new Image_Font($path);
299: $this->font = $font->utilise();
300: }
301:
302: 303: 304: 305: 306: 307: 308: 309:
310: public function setColor($r,$v,$b)
311: {
312: return $this->couleur = imagecolorallocate($this->source,$r,$v,$b);
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: public function setBgColor($r,$v,$b)
323: {
324: imagefill($this->source,0,0,$this->setColor($r,$v,$b));
325: }
326:
327: 328: 329: 330: 331:
332: public function setType($type)
333: {
334: if($this->type == null)
335: $this->type = intval($type);
336: }
337:
338: 339: 340: 341: 342: 343: 344: 345: 346: 347:
348: public function setText($texte,$size,$x,$y,$angle=0,$rect=false)
349: {
350: if($rect)
351: {
352: $rectText = imageftbbox($size,0,$this->font,$texte);
353: $wText = abs($rectText[4]-$rectText[0]);
354: $hText = abs($rectText[1]-$rectText[5]);
355: $this->setColor(255,255,255);
356: imagefilledrectangle($this->source,$x,($y-$hText),($x+$wText+5),($y+5),$this->couleur);
357:
358: $this->setColor(0,0,0);
359: imagettftext($this->source,$size,$angle,$x,$y,$this->couleur,$this->font,$texte);
360: }
361: else
362: {
363: imagettftext($this->source,$size,$angle,$x,$y,$this->couleur,$this->font,$texte);
364: }
365: }
366:
367: 368: 369: 370: 371: 372: 373:
374: public function rotate($angle)
375: {
376: $this->source = imagerotate($this->source,$angle,-1);
377: }
378:
379: 380: 381: 382: 383: 384: 385:
386: public function resize($newW,$newH)
387: {
388: if($newW == 0)
389: {
390: $scale = $newH/$this->height;
391: $newW = $this->width * $scale;
392: }
393: elseif($newH == 0)
394: {
395: $scale = $newW / $this->width;
396: $newH = $this->height * $scale;
397: }
398: $tempImg = imagecreatetruecolor($newW,$newH);
399:
400:
401: if(self::IMG_GIF == $this->type)
402: {
403: imagealphablending($tempImg,false);
404: imagesavealpha($tempImg,true);
405: $transparence = imagecolorallocatealpha($tempImg,255,255,255,127);
406: imagefilledrectangle($tempImg,0,0,$newW,$newH,$transparence);
407: imagecolortransparent($tempImg,$transparence);
408: }
409:
410: if(self::IMG_PNG == $this->type)
411: {
412: $background = imagecolorallocate($tempImg, 0, 0, 0);
413: imagecolortransparent($tempImg,$background);
414: imagealphablending($tempImg, false);
415: }
416:
417: if(imagecopyresampled($tempImg,$this->source,0,0,0,0,$newW,$newH,$this->width,$this->height))
418: {
419: $this->source = $tempImg;
420: $this->width = $newW;
421: $this->height = $newH;
422: return true;
423: }
424: else
425: {
426: return false;
427: }
428: }
429: 430: 431: 432: 433: 434: 435: 436: 437:
438: public function miniaturise($newW,$newH,$color="#000000")
439: {
440: $rgb = $this->hexToRgb($color);
441: $tempImg = imagecreatetruecolor($newW,$newH);
442: $color = imagecolorallocate($tempImg,$rgb[0],$rgb[1],$rgb[2]);
443: imagefill($tempImg,0,0,$color);
444:
445: if($this->width == max($this->width,$this->height))
446: {
447: $this->resize($newW,null);
448: $dH = $newH - $this->height;
449: if(imagecopy($tempImg,$this->source,0,($dH/2),0,0,$this->width,$this->height))
450: $this->source = $tempImg;
451: else
452: throw new RuntimeException('Création de la miniature impossible');
453: }
454: else
455: {
456: $this->resize(null,$newH);
457: $dW = $newW - $this->width;
458: if(imagecopy($tempImg,$this->source,($dW/2),0,0,0,$this->width,$this->height))
459: $this->source = $tempImg;
460: else
461: throw new RuntimeException('Création de la miniature impossible');
462: }
463:
464: }
465:
466: 467: 468: 469: 470: 471: 472: 473: 474:
475: public function crop($cropW,$cropH,$cropStartX,$cropStartY)
476: {
477: $tempImg = imagecreatetruecolor($cropW,$cropH);
478:
479: if(self::IMG_GIF == $this->type)
480: {
481: imagealphablending($tempImg,false);
482: imagesavealpha($tempImg,true);
483: $transparence = imagecolorallocatealpha($tempImg,255,255,255,127);
484: imagefilledrectangle($tempImg,0,0,$newW,$newH,$transparence);
485: imagecolortransparent($tempImg,$transparence);
486: }
487:
488: if(imagecopyresized($tempImg, $this->source, 0, 0, $cropStartX, $cropStartY, $cropW, $cropH, $cropW, $cropH))
489: {
490: $this->source = $tempImg;
491: $this->width = $cropW;
492: $this->height = $cropH;
493: return true;
494: }
495: else
496: {
497: return false;
498: }
499: }
500:
501: 502: 503: 504: 505: 506: 507:
508: public static function hexToRgb($color)
509: {
510: if ($color[0]=='#')
511: {
512: $color = substr($color, 1);
513: }
514: else if ($color[1]=='x')
515: {
516: $color = substr($color, 2);
517: }
518: $rgb = array();
519: $rgb[0] = hexdec(substr($color,0,2));
520: $rgb[1] = hexdec(substr($color,2,2));
521: $rgb[2] = hexdec(substr($color,4,2));
522: return $rgb;
523: }
524:
525:
526: 527: 528: 529: 530: 531: 532:
533: public static function RgbToHex($rgb)
534: {
535: for($i=0;$i<2;$i++)
536: {
537: if($rgb[$i]<0 || $rgb[$i]>255)
538: throw new Image_Exception('La valeur RGB est incorrecte (compris en 0 et 255');
539: }
540:
541: return str_pad((dechex($rgb[0]).dechex($rgb[1]).dechex($rgb[2])),6,"0",STR_PAD_LEFT);
542: }
543:
544: 545: 546: 547: 548: 549:
550: public function setBorder($border,$color)
551: {
552: $couleur = $this->hexToRgb($color);
553: $this->setColor($couleur[0],$couleur[1],$couleur[2]);
554:
555: imagefilledrectangle($this->source,0,0,$border,$this->height,$this->couleur);
556:
557: imagefilledrectangle($this->source,$this->width-$border,0,$this->width,$this->height,$this->couleur);
558:
559: imagefilledrectangle($this->source,0,0,$this->width,$border,$this->couleur);
560:
561: imagefilledrectangle($this->source,0,$this->height - $border,$this->width,$this->height,$this->couleur);
562:
563: }
564:
565: 566: 567: 568: 569: 570:
571: public function save($file,$qualite=95)
572: {
573: if($this->type==self::IMG_GIF)
574: {
575: if(imagegif($this->source,$file))
576: return true;
577: else
578: return false;
579: }
580: elseif($this->type == self::IMG_JPG)
581: {
582: if(imagejpeg($this->source,$file,$qualite))
583: return true;
584: else
585: return false;
586: }
587: elseif($this->type == self::IMG_PNG)
588: {
589: if(imagepng($this->source,$file))
590: return true;
591: else
592: return false;
593: }
594: }
595:
596: 597: 598: 599: 600:
601: public function display($qualite=100)
602: {
603: if($this->type== self::IMG_GIF)
604: {
605: header("Content-type: image/gif");
606: return imagegif($this->source);
607: }
608: elseif($this->type == self::IMG_JPG)
609: {
610: header("Content-type: image/jpeg");
611: return imagejpeg($this->source,'',$qualite);
612: }
613: elseif($this->type == self::IMG_PNG)
614: {
615: header("Content-type: image/png");
616: return imagepng($this->source);
617: }
618: }
619:
620: 621: 622: 623: 624: 625:
626: public function getSource()
627: {
628: return $this->source;
629: }
630:
631: 632: 633: 634: 635: 636:
637: public function setSource($resource)
638: {
639: if($resource!=null && is_resource($resource))
640: $this->source = $resource;
641: else
642: throw new Image_Exception('La ressource n est pas valide.');
643: }
644:
645: 646: 647:
648: public function __destruct()
649: {
650: if(is_resource($this->source))
651: imagedestroy($this->source);
652: @imagedestroy($tempImg);
653: if($this->copie!=null)
654: @imagedestroy($this->copie);
655: }
656: }
657: ?>