Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Image_Captcha
  • Image_Converter
  • Image_DegradeCouleur
  • Image_Font
  • Image_Gauge
  • Image_Image
  • Image_Traitement

Exceptions

  • Image_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Gestion de base des images
 15:  * @category Pry
 16:  * @package Image
 17:  * @version 1.5.0 
 18:  * @author Olivier ROGER <oroger.fr>  
 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:      * @access protected
 34:      * @var int
 35:      */
 36:      protected $width;
 37:     
 38:     /**
 39:      * @var int
 40:      * @access protected
 41:      */
 42:      protected $height;
 43:      
 44:      /**
 45:      * Type de l image : 1 = gif ; 2 = jpeg; 3= png
 46:      * @var string 
 47:      * @access protected
 48:      * 
 49:      */
 50:      protected $type;
 51:      
 52:      /**
 53:      * @var string $mime Type mime de l image
 54:      * @access protected
 55:      */
 56:      protected $mime;
 57:      /**
 58:      * @var img Fichier image source
 59:      * @access protected
 60:      */
 61:      protected $source;
 62:     
 63:     /**
 64:      * @var string Police d'ecriture selectionnee. Defaut verdanna.
 65:      * @access private
 66:      */
 67:      protected $font;
 68:      
 69:      /**
 70:       * @var ressource Couleur choisi (par defaut bordeau)
 71:       * @access protected
 72:       */
 73:      protected $couleur;
 74:      
 75:      /**
 76:       * @var array Info de l'image
 77:       * @access protected
 78:       */
 79:      protected $infoImage;
 80:      
 81:      /**
 82:       * @var int Poids de l'image
 83:       * @access protected
 84:       */
 85:      protected $poids;
 86:      
 87:      private $copie;
 88:      
 89:      /**
 90:       * @var Image_Image autre image a coller sur l image d origine
 91:       * @access private
 92:       */
 93:      private $logo;
 94:      /**
 95:       * @var ressource Largeur du logo
 96:       * @access private
 97:       */
 98:      private $widthL;
 99:      /**
100:       * @var ressource Hauteur du logo
101:       * @access private
102:       */
103:      private $heightL;
104:      /**
105:       * @var ressource Type du logo
106:       * @access private
107:       */
108:      private $typeL;
109:      
110: 
111:     /**
112:      * Construteur
113:      * @param string $img Chemin vers l'image
114:      * @param int $w Largeur a fournir si aucune image source
115:      * @param int $h Hauteur a fournir si aucune image source
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]; // 1:gif; 2:jpg; 3:png; 4:swf; 5:psd; 6:bmp; 7:tiff
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); // Récupére l'extension après le .
143:             $this->infoImage['extension']   = strtolower($this->infoImage['extension']);
144:         }
145:         else
146:         {
147:             /**
148:              * Gestion de création d'image vierge
149:              * @since 1.2.0
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:      * Récupére les informations de l'image
171:      * @access public
172:      * @return array Information de l'image (reso,poids,extension,mime)
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:      * Ajoute une image en tant que logo
186:      * @access public
187:      * @param string chemin vers l'image
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:      * Ajoute le logo à l'image principale
203:      * @access public
204:      * @param string $pos Position du logo : ct(centre),hg(haut gauche),hd,bg(bas gauche),bd(défaut)
205:      * @param int $opacite % d'opacité du logo , par défaut 75
206:      * @return bool 
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:      * Créer une ressource image selon son type
244:      * @access protected
245:      * @param string image
246:      * @return ressource image
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:       * Créer une copie de l image original pour restauration ulterieur.
274:       * @access public
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:       * Restaure la copie de sauvegarde de l'image
284:       * @access public
285:       */
286:       public function restore()
287:       {
288:         $this->source = $this->copie;
289:       }
290:      
291:      /**
292:       * Permet le changement de police. indiquer le chemin vers le fichier ttf
293:       * @access public
294:       * @param string $path Chemin vers la police ou non de la police dans le dossier /font
295:       */
296:       public function setFont($path)
297:       {
298:         $font = new Image_Font($path);
299:         $this->font = $font->utilise();
300:       }
301:       
302:      /**
303:       * Permet de définir une couleur à utiliser
304:       * @access public
305:       * @param int $r composante rouge
306:       * @param int $v composante verte
307:       * @param int $b composante bleue
308:       * @return int
309:       */
310:     public function setColor($r,$v,$b)
311:     {
312:         return $this->couleur = imagecolorallocate($this->source,$r,$v,$b);
313:     }
314:     
315:     /**
316:      * Remplie le fond d'une image avec une couleur
317:      * @since 1.2.0
318:      * @param int $r Composante Rouge
319:      * @param int $v Composante Verte
320:      * @param int $b Composante Bleu
321:      */
322:     public function setBgColor($r,$v,$b)
323:     {
324:         imagefill($this->source,0,0,$this->setColor($r,$v,$b));
325:     }
326:     
327:     /**
328:      * Determine le type d'image voulu
329:      * @since 1.2.0
330:      * @param int $type
331:      */
332:     public function setType($type)
333:     {
334:         if($this->type == null)
335:             $this->type = intval($type);   
336:     }
337:     
338:    /**
339:     * Ecrit un texte sur l image aux positions données
340:     * @access public
341:     * @param string $texte Texte à afficher
342:     * @param int $size Taille du texte
343:     * @param int $x Position en X
344:     * @param int $y Position en Y
345:     * @param int $angle Inclinaison du texte
346:     * @param bool $rect Ajout ou non d'un rectangle blanc sous le texte
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); // Retourne les coordoonées du text
353:             $wText    = abs($rectText[4]-$rectText[0]); // Largeur du texte
354:             $hText    = abs($rectText[1]-$rectText[5]); // hauteur du texte
355:             $this->setColor(255,255,255); // Fond blanc
356:             imagefilledrectangle($this->source,$x,($y-$hText),($x+$wText+5),($y+5),$this->couleur); //($y-$htext) permet de placer
357:                                                                                                     // le rectangle au bon endroit
358:             $this->setColor(0,0,0); // Texte noir
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:      * Permet la rotation d'une image
369:      *
370:      * @param int $angle
371:      * @since 1.4.1
372:      * @access public
373:      */
374:     public function rotate($angle)
375:     {
376:         $this->source = imagerotate($this->source,$angle,-1);
377:     }
378:     
379:    /**
380:    * Redimensionne l'image. Si une des deux dimension = 0. Redimensionnement proportionnel sur celle donnée
381:    * @param int $newW Largeur souahitée
382:    * @param int $newH Hauteur souhaitée
383:    * @access public
384:    * @return bool
385:    */
386:     public function resize($newW,$newH)
387:     {
388:         if($newW == 0) // Largeur non spécifiée donc dimension basé sur hauteur
389:         {
390:             $scale = $newH/$this->height;
391:             $newW  = $this->width * $scale;
392:         }
393:         elseif($newH == 0) // Hauteur non spécifiée donc dimension basé sur largeur
394:         {
395:             $scale = $newW / $this->width;
396:             $newH  = $this->height * $scale;
397:         }
398:         $tempImg = imagecreatetruecolor($newW,$newH);
399:         
400:         //Evite fond noir avec la transparence GIF
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); // Image temp totalement transparente
414:             imagealphablending($tempImg, false); // Pas d'alpha blending pour garder le channel alpha
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:      * Créer une miniature de l'image source.
431:      * Si l'image n'a pas le même format que la miniature , des bandes noires apparaitrons.
432:      *
433:      * @param int $newW Largeur de la miniature
434:      * @param int $newH Hauteur de la miniature
435:      * @param string $color Couleur en hexa du fond de la miniature
436:      * @since 1.4.5
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:         //La largeur est la plus grande valeur
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:      * Crop une image aux dimensions voulues et à partir de l'endroit voulu
468:      *
469:      * @param int $cropW Largeur de la zone de crop
470:      * @param int $cropH Hauteur de la zone de crop
471:      * @param int $cropStartX Coordonnées en X de départ
472:      * @param int $cropStartY Coordonnées en Y de départ
473:      * @return bool
474:      */
475:     public function crop($cropW,$cropH,$cropStartX,$cropStartY)
476:     {
477:         $tempImg = imagecreatetruecolor($cropW,$cropH);
478:         //Evite le fond noir
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:      * Convertit une valeur hexadecimal en couleur RGB
503:      *
504:      * @param string $color Couleur hexa nettoyer de tout caractère supplémentaires (0x,#,...)
505:      * @since 1.2.0
506:      * @return array
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:      * Convertit une valeur RGB en valeur hexa
528:      *
529:      * @param array $rgb Tableau des valeurs rgb array(45,49,176);
530:      * @since 1.2.0
531:      * @return string
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:      * Créee une bordure autour de l'image
546:      *
547:      * @param int $border Taille en px de la bordure
548:      * @param string $color Couleur hexa de la bordure (#FFFFFF ou 0xFFFFFF)
549:      */
550:     public function setBorder($border,$color)
551:     {
552:         $couleur = $this->hexToRgb($color);
553:         $this->setColor($couleur[0],$couleur[1],$couleur[2]);
554:         // Trait vertical gauche
555:         imagefilledrectangle($this->source,0,0,$border,$this->height,$this->couleur);
556:         // Trait vertical droit
557:         imagefilledrectangle($this->source,$this->width-$border,0,$this->width,$this->height,$this->couleur);
558:         // Trait horizontal haut
559:         imagefilledrectangle($this->source,0,0,$this->width,$border,$this->couleur);
560:         //Trait horizontal bas
561:         imagefilledrectangle($this->source,0,$this->height - $border,$this->width,$this->height,$this->couleur);
562:         
563:     }
564:     
565:       /**
566:       * Sauvegarde l'image sur le disque
567:       * @access public
568:       * @param string $file Nom et chemin de fichier
569:       * @return bool
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:       * Affiche l'image sur la sortie standard
598:       * @access public
599:       * @return img
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:       * Getter pour la ressource image
622:       * 
623:       * @since 1.3.0
624:       * @return resource
625:       */
626:      public function getSource()
627:      {
628:         return $this->source;
629:      }
630:      
631:      /**
632:       * Setter pour la resource image
633:       *
634:       * @param resource $resource
635:       * @since 1.3.0
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:      * Destructeur
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: ?>
Pry Framework API documentation generated by ApiGen 2.6.1