Page 1 sur 1

Classe image

Posté : 25 déc. 2009, 22:12
par djtec
Bonsoir,

Voilà j'ai fait y a quelque temps avec l'aide de Sadeg une classe image, j'ai voulu rajouter des fonctions dans cette classe mais sa fonctionne pas très bien.

Voici ma classe:
<?php

class Image {
	
	/*----------------------------------------------
	@
	@ Déclaration des variables
	@
	----------------------------------------------*/
	
	private $filename;
	private $tofile;
	private $toext;
	
	public $image_source;
	public $image_ressource;
	public $image_copy;
	
	
	var $width;
	var $height;
	var $__reflet;
	var $__border = array();
	var $__text = array();
	var $__logo = array();
	var $__croppercent = array();
	var $__croppercentcenter = array();
	var $__cropcenter = array();
	var $__crop = array();
	var $__resize = array();
	
	
	
	/*----------------------------------------------
	@
	@ Déclaration pour le fonctionnement
	@	de la classe
	@
	----------------------------------------------*/
	
    /*
    @ Constructeur de la classe
    */
    public function __construct($filename = null, $tofile = null, $toext = null) {
		$this->filename = $filename;
		$this->image_source = $filename;
		$this->tofile = $tofile;
		$this->toext = $toext;
	}
	
	
    /*
    @ Déconstructeur de la classe
    */
    public function __destruct() {
		imagedestroy($this->image_ressource);
		imagedestroy($this->image_copy);
	}
	
	
	
	
	/*----------------------------------------------
	@
	@ Fonction utile à la classe
	@
	----------------------------------------------*/
	
    	/*
    	@ Fonction de création d'une ressource de travail pour l'image
    	*/
		private function getRessource() {
			if($this->toext) {
				switch($this->toext) {
                	case "gif":
						return imagecreatefromgif($this->image_source);
					break;
				
                	case "jpg":
					case "jpeg":
						return imagecreatefromjpeg($this->image_source);
					break;
				
                	case "png":
						return imagecreatefrompng($this->image_source);
					break;
            	}
			} else {
    			switch($this->__setType()) {
					case 1:
						return imagecreatefromgif($this->image_source);
					break;
				
					case 2:
						return imagecreatefromjpeg($this->image_source);
					break;
				
					case 3:
						return imagecreatefrompng($this->image_source);
					break;
				}
			}
		}
	
	
		/*
		@ Fonction de demarrage de la ressource de travail pour l'image
		*/
		private function getStart() {
			$size = getimagesize($this->image_source);
			
			if(!$this->width) {
				$this->width = $size[0];
			}
			
			if(!$this->height) {
				$this->height = $size[1];
			}
			
			$this->image_copy = imagecreatetruecolor($this->width, $this->height);
		}
	
	
		/*
		@ Fonction de convertion de couleur Hex en RBG
		*/
		private function getHexToRgb($color){
 			$color_array = array();
 			$hex_color = strtoupper($color);
 			
			for($i = 0; $i < 6; $i++) {
  				$hex = substr($hex_color, $i, 1);
 					
					switch($hex){
   						case "A": 
							$num = 10; 
						break;
						
   						case "B": 
							$num = 11; 
						break;
						
   						case "C": 
							$num = 12; 
						break;
						
   						case "D": 
							$num = 13; 
						break;
						
   						case "E": 
							$num = 14; 
						break;
						
   						case "F": 
							$num = 15; 
						break;
						
   						default: 
							$num = $hex; 
						break;
  					}
					
  				array_push($color_array, $num);
 			}
			
 			$R = (($color_array[0] * 16) + $color_array[1]);
 			$G = (($color_array[2] * 16) + $color_array[3]);
 			$B = (($color_array[4] * 16) + $color_array[5]);
			
 			return array($R, $G, $B);
			
 			unset($color_array, $hex, $R, $G, $B);
		} 
		
		
		/*
		@ Fonction de reflet de l'image
		*/
		private function getReflet() {
			imagealphablending($this->image_copy, true);

			$colorToPaint = imagecolorallocatealpha($this->image_copy, 255, 255, 255, 0);
			imagefilledrectangle($this->image_copy, 0, 0, $this->width, $this->newHeight, $colorToPaint);
			
			imagecopyresampled($this->image_copy, $this->image_ressource, 0, 0, 0, $this->reflectedPart, $this->width, $this->reflectionHeight, $this->width, ($this->height - $this->reflectedPart));

			$x_i = imagesx($this->image_copy);
			$y_i = imagesy($this->image_copy);
			
			for($x = 0; $x < $x_i; $x++) {
    			for($y = 0; $y < $y_i; $y++) {
        			imagecopy($this->image_copy, $this->image_copy, $x, $y_i - $y - 1, $x, $y, 1, 1);
   				}
			}
    		
			imagecopy($this->image_copy, $this->image_ressource, 0, 0, 0, 0, $this->width , $this->height - $this->reflectionHeight);

			imagealphablending($this->image_copy, true);

			for($i = 0; $i < $this->reflectionHeight; $i++) {
    			$colorToPaint = imagecolorallocatealpha($this->image_copy, 255, 255, 255, ($i / $this->reflectionHeight * -1 + 1) * $this->white);
    			imagefilledrectangle($this->image_copy, 0, $this->height + $i, $this->width, $this->height + $i, $colorToPaint);
			}
		}
	
	
		/*
		@ Fonction de bordure de l'image
		*/
		private function getBorder() {
			$rgb = $this->getHexToRgb($this->__border[0]["color"]);
			
			$colorToPaint = imagecolorallocate($this->image_copy, $rgb[0], $rgb[1], $rgb[2]);
			
			imagesetthickness($this->image_copy, $this->__border[0]["ep"]); // Epaisseur des lignes

			imageline($this->image_copy, 0, 0, $this->width, 0, $colorToPaint); //top line
			imageline($this->image_copy, 0, $this->height, $this->width, $this->height, $colorToPaint); //bottom line
			imageline($this->image_copy, 0, 0, 0, $this->height, $colorToPaint); //left line
			imageline($this->image_copy, $this->width - 1 , 0, $this->width - 1, $this->height, $colorToPaint); //right line
		}
	
	
		/*
		@ Fonction d'écriture de texte sur l'image
		*/
		private function getText($i) {
			$text = $this->__text[$i];
			
			$rgb = $this->getHexToRgb($text["color"]);
			
			$colorToPaint = imagecolorallocate($this->image_copy, $rgb[0], $rgb[1], $rgb[2]);
			
			//imagestring($this->image_copy, 2, $y, $x, $text, $colorToPaint);
			imagettftext($this->image_copy, $text["size"], 0, $text["x"], $text["y"], $colorToPaint, $text["font"], $text["text"]);
		}
		
	
	
		/*
		@ Fonction de crop sur l'image
		*/
		private function getCrop() {
		}
		
	
	
		/*
		@ Fonction de redimmension de l'image
		*/
		private function getResize() {	
		}
	
	
	
	
	/*----------------------------------------------
	@
	@ Fonction lancé par l'utilisateur
	@
	----------------------------------------------*/
	
		/*
		@ Fonction d'entête de l'image
		*/
		public function setEnTete() {
			$size = getimagesize($this->image_source);
			
			$this->image_ressource = $this->getRessource();
			
			return header('Content-type: '.$size['mime']);
		}
	
	
		/*
		@ Fonction de sauvegarde de l'image
		*/
		public function setGenerated() {
			$this->getStart();
			
			imagecopy($this->image_copy, $this->image_ressource, 0, 0, 0, 0, $this->width, $this->height);
			
			if($this->__reflet) {
                $this->getReflet();
            }
			
			if($this->__border) {
                $this->getBorder();
            }
			
			if($this->__text) {
                for($i = 0; $i < count($this->__text); $i++) {
					$this->getText($i);
				}
            }
		}
		
		
		/*
		@ Fonction de sauvegarde de l'image
		*/
		public function setSave() {
			if($this->toext) {
				switch($this->toext) {
                	case "gif":
						return imagegif($this->image_copy, $this->tofile.".".$this->toext);
					break;
				
                	case "jpg":
					case "jpeg":
						return imagejpeg($this->image_copy, $this->tofile.".".$this->toext, 80);
					break;
				
                	case "png":
						return imagepng($this->image_copy, $this->tofile.".".$this->toext);
					break;
            	}
			} else {
				switch($this->__setType()) {
                	case 1:
						return imagegif($this->image_copy, "");
					break;
				
                	case 2:
						return imagejpeg($this->image_copy, "", 80);
					break;
				
                	case 3:
						return imagepng($this->image_copy, "");
					break;
            	}
			}
		}
		
		
		/*
		@ Fonction de reflet de l'image
		*/
		public function addReflet($percent = 60, $reflection = 60, $white = 80) {
			$size = getimagesize($this->image_source);
			
			$this->reflectionHeight = intval($size[1] * ($reflection / 100));
			$this->newHeight = $size[1] + $this->reflectionHeight;
			$this->reflectedPart = $size[1] * ($percent / 100);

			$this->height = $this->newHeight;
			
			$this->white = $white;
			
			$this->__reflet = true;
		}
	
	
		/*
		@ Fonction de bordure de l'image
		*/
		public function addBorder($color, $ep = 1, $type = null) {
			$this->height = $this->height + $ep;
			
			$this->__border[] = array(
				'color' => $color, 
				'ep' => $ep, 
				'type' => $type
			);
		}
	
	
		/*
		@ Fonction d'écriture de texte sur l'image
		*/
		public function addText($text, $font, $size = 10, $color, $x = 50, $y = 100) {
			$this->__text[] = array(
				'text' => $text, 
				'color' => $color,
				'font' => $font, 
				'size' => $size, 
				'x' => $x, 
				'y' => $y
			);
		}
		
	
	
		/*
		@ Fonction d'ajout de logo sur l'image
		*/
		public function addLogo($logo, $x = 50, $y = 100) {
			$this->__logo[] = array(
				'logo' => $logo, 
				'x' => $x, 
				'y' => $y
			);
		}
		
	
	
		/*
		@ Fonction de crop sur l'image
		*/
		public function addCrop($height, $width, $x = 50, $y = 100, $percent = 0, $center = false) {
			if($center == false && $percent != 0) {
				$this->__croppercent[] = array(
					'height' => $height, 
					'width' => $width, 
					'percent' => $percent
				);
			} elseif($center != false && $percent != 0) {
				$this->__croppercentcenter[] = array(
					'height' => $height, 
					'width' => $width, 
					'percent' => $percent
				);
			} elseif($center != false && $percent == 0) {
				$this->__cropcenter[] = array(
					'height' => $height, 
					'width' => $width
				);
			} else {
				$this->__crop[] = array(
					'height' => $height, 
					'width' => $width, 
					'x' => $x, 
					'y' => $y
				);
			}
		}
		
	
	
		/*
		@ Fonction de redimmension de l'image
		*/
		public function addResize($height, $witdh, $auto = false, $percent = 0) {
			$this->__resize[] = array(
				'height' => $height, 
				'width' => $width, 
				'auto' => $auto, 
				'percent' => $percent
			);
		}
	
	
	
	
	/*----------------------------------------------
	@
	@ Information sur le fichier source
	@
	----------------------------------------------*/
	
    /*
    @ Info -> Nom du fichier
    */
	public function __setName() {
		$fileInfo = pathinfo($this->image_source);
		
		return $fileInfo['basename'];
	}
	
	
    /*
    @ Info -> Largeur du fichier
    */
	public function __setWidth() {
		$size = getimagesize($this->image_source);
		
		return $size[0];
	}
	
	
    /*
    @ Info -> Hauteur du fichier
    */
	public function __setHeight() {
		$size = getimagesize($this->image_source);
		
		return $size[1];
	}
	
	
    /*
    @ Info -> Poids du fichier en Ko
    */
	public function __setSize() {
		return filesize($this->image_source);
	}
	
	
    /*
    @ Info -> Type de fichier
    */
	public function __setType() {
		$size = getimagesize($this->image_source);
		
		return $size[2];
	}
	
	
    /*
    @ Info -> Path du fichier
    */
	public function __setPath() {
		$fileInfo = pathinfo($this->image_source);
		
		return $fileInfo['dirname'];
	}
	
	
    /*
    @ Info -> Extension du fichier
    */
	public function __setExt() {
        $fileInfo = pathinfo($this->image_source);

    	return $fileInfo['extension'];
	}
	
}

?>
Et voici comment je l'utilise:
<?php

include("class.image3.php");

$i = new Image("images/funkyblue-vector.png", "vector", "png");

//$i->addBorder("01FF2C", 10);
$i->addReflet(40, 40, 20);
//$i->addText("Voici un test d'un texte", "arial.ttf", 10, "01FF2C", 50, 100);
//$i->addText("Voici un test d'un texte 2", "arial.ttf", 15, "01FF2C", 80, 200);

$i->setEnTete();
$i->setGenerated();
$i->setSave();

?>
L'ajout de texte et de bordure fonctionne très bien.

Par-contre la fonction reflet ne fonctionne pas très bien.

Voici des images pour illustrer le problème:

Voici l'image original: http://img707.imageshack.us/img707/5386 ... vector.png
Voici l'image généré par ma classe: http://img268.imageshack.us/img268/5516/vectoro.png
Et voici le résultat qui devrait être perçu par la classe: http://img192.imageshack.us/img192/1328/vector2.png

Le résultat final est obtenu par se script:
<?php
header ("Content-type: image/png");

$filename = imagecreatefrompng("images/funkyblue-vector.png");

$percent = 60;
$reflection = 40;
$white = 80;

$width = imagesx($filename);
$height = imagesy($filename);

$reflectionHeight = intval($height * ($reflection / 100));
$newHeight = $height + $reflectionHeight;
$reflectedPart = $height * ($percent / 100);

$new_img = imagecreatetruecolor($width, $newHeight);

imagealphablending($new_img, true);

$colorToPaint = imagecolorallocatealpha($new_img, 255, 255, 255, 0);
imagefilledrectangle($new_img, 0, 0, $width, $newHeight, $colorToPaint);

imagecopyresampled($new_img, $filename, 0, 0, 0, $reflectedPart, $width, $reflectionHeight, $width, ($height - $reflectedPart));

$x_i = imagesx($new_img);
$y_i = imagesy($new_img);

for($x = 0; $x < $x_i; $x++) 
{
    for($y = 0; $y < $y_i; $y++) 
    {
        imagecopy($new_img, $new_img, $x, $y_i - $y - 1, $x, $y, 1, 1);
    }
}
    
imagecopy($new_img, $filename, 0, 0, 0, 0, $width, $height);

imagealphablending($new_img, true);

for($i = 0; $i < $reflectionHeight; $i++) 
{
    $colorToPaint = imagecolorallocatealpha($new_img, 255, 255, 255, ($i/$reflectionHeight*-1+1)*$white);
    imagefilledrectangle($new_img, 0, $height+$i, $width, $height+$i, $colorToPaint);
}

imagepng($new_img, "vector2.png");
?>
Voila j'ai pris le script et je les mis dans la classe mais ça bug et je vois pas d'où cela peut venir.

Et y a aussi que l'image ne s'affiche pas dans ma page sa me met dans la source "l'image ne peut être affiché car elle contient des erreurs" pourtant l'image est bien sauvegardé.

Donc merci d'avance de m'aider...

++

Re: Classe image

Posté : 11 janv. 2010, 00:31
par djtec
ptit up