Reflet d'image

Eléphant du PHP | 451 Messages

24 sept. 2008, 00:14

Bonsoir,

Voilà j'ai développé une classe image ici qui gère la réduction d'image et la rotation d'image mais j'aimerais faire un reflet des images.

J'ai vu une classe qui fesait le reflet d'image.

Url du site: http://blog.idzeo.fr/index.php?2007/01/ ... -class-v20

J'ai essayé d'extraire le script de reflet mais en le modifiant chez moi ça fait un zoom d'une partie de l'image et le reflet ne se fait pas du tout.

Voici le script original:
	/**
	 * Creates Apple-style reflection under image, optionally adding a border to main image
	 *
	 * @param int $percent
	 * @param int $reflection
	 * @param int $white
	 * @param bool $border
	 * @param string $borderColor
	 */
	public function createReflection($percent,$reflection,$white,$border = true,$borderColor = '#a4a4a4') {
        $width = $this->currentDimensions['width'];
        $height = $this->currentDimensions['height'];

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

        $this->workingImage = ImageCreateTrueColor($width,$newHeight);

        ImageAlphaBlending($this->workingImage,true);

        $colorToPaint = ImageColorAllocateAlpha($this->workingImage,255,255,255,0);
        ImageFilledRectangle($this->workingImage,0,0,$width,$newHeight,$colorToPaint);

        imagecopyresampled(
                            $this->workingImage,
                            $this->newImage,
                            0,
                            0,
                            0,
                            $reflectedPart,
                            $width,
                            $reflectionHeight,
                            $width,
                            ($height - $reflectedPart));
        $this->imageFlipVertical();

        imagecopy($this->workingImage,$this->newImage,0,0,0,0,$width,$height);

        imagealphablending($this->workingImage,true);

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

        if($border == true) {
            $rgb = $this->hex2rgb($borderColor,false);
            $colorToPaint = imagecolorallocate($this->workingImage,$rgb[0],$rgb[1],$rgb[2]);
            imageline($this->workingImage,0,0,$width,0,$colorToPaint); //top line
            imageline($this->workingImage,0,$height,$width,$height,$colorToPaint); //bottom line
            imageline($this->workingImage,0,0,0,$height,$colorToPaint); //left line
            imageline($this->workingImage,$width-1,0,$width-1,$height,$colorToPaint); //right line
        }

        $this->oldImage = $this->workingImage;
		$this->newImage = $this->workingImage;
		$this->currentDimensions['width'] = $width;
		$this->currentDimensions['height'] = $newHeight;
	}

	/**
	 * Inverts working image, used by reflection function
	 * 
	 */
	private function imageFlipVertical() {
	    $x_i = imagesx($this->workingImage);
	    $y_i = imagesy($this->workingImage);

	    for($x = 0; $x < $x_i; $x++) {
	        for($y = 0; $y < $y_i; $y++) {
	            imagecopy($this->workingImage,$this->workingImage,$x,$y_i - $y - 1, $x, $y, 1, 1);
	        }
	    }
	}

	/**
	 * Converts hexidecimal color value to rgb values and returns as array/string
	 *
	 * @param string $hex
	 * @param bool $asString
	 * @return array|string
	 */
	private function hex2rgb($hex, $asString = false) {
        // strip off any leading #
        if (0 === strpos($hex, '#')) {
           $hex = substr($hex, 1);
        } else if (0 === strpos($hex, '&H')) {
           $hex = substr($hex, 2);
        }

        // break into hex 3-tuple
        $cutpoint = ceil(strlen($hex) / 2)-1;
        $rgb = explode(':', wordwrap($hex, $cutpoint, ':', $cutpoint), 3);

        // convert each tuple to decimal
        $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0);
        $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0);
        $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0);

        return ($asString ? "{$rgb[0]} {$rgb[1]} {$rgb[2]}" : $rgb);
    }
A utiliser comme ceci:
$thumb->createReflection(40,40,80,true,'#a4a4a4');

Et voici ce que j'ai fait:
    /*
    @ Fonction qui retourne l'image
    */
	private function imageFlipVertical() 
	{
	    $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);
	        }
	    }
	}

    /*
    @ Fonction qui convertit en RGB
    */
	private function hex2rgb($hex, $asString = false) 
	{
        // strip off any leading #
        if (0 === strpos($hex, '#')) 
		{
           $hex = substr($hex, 1);
        } 
		else if (0 === strpos($hex, '&H')) 
		{
           $hex = substr($hex, 2);
        }

        // break into hex 3-tuple
        $cutpoint = ceil(strlen($hex) / 2)-1;
        $rgb = explode(':', wordwrap($hex, $cutpoint, ':', $cutpoint), 3);

        // convert each tuple to decimal
        $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0);
        $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0);
        $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0);

        return ($asString ? "{$rgb[0]} {$rgb[1]} {$rgb[2]}" : $rgb);
    }

    /*
    @ Fonctions de création d'un reflet pour l'image
    */
	public function reflection($type_resize, $percent, $reflection, $white, $border = true, $borderColor = '#a4a4a4') 
	{
        $reflectionHeight = intval($max_height * ($reflection / 100));
        $newHeight = $max_height + $reflectionHeight;
        $reflectedPart = $max_height * ($percent / 100);

        // Déterminer la taille de redimensionnement
        switch (strtolower(trim($type_resize)))
        {
            case 'small': 
                $max_width = self::MAX_SMALL_WIDTH;
                $max_height = self::MAX_SMALL_HEIGHT;
				
				$coordonnee_x = self::TEXT_CORD_X_SMALL;
				$coordonnee_y = self::TEXT_CORD_Y_SMALL;
            break;
			
            case 'medium': 
                $max_width = self::MAX_MEDIUM_WIDTH;
                $max_height = self::MAX_MEDIUM_HEIGHT;

				$coordonnee_x = self::TEXT_CORD_X_MEDIUM;
				$coordonnee_y = self::TEXT_CORD_Y_MEDIUM;
            break;
			
            case 'big': 
                $max_width = self::MAX_BIG_WIDTH;
                $max_height = self::MAX_BIG_HEIGHT;

				$coordonnee_x = self::TEXT_CORD_X_BIG;
				$coordonnee_y = self::TEXT_CORD_Y_BIG;
            break;
			
            default: // taille normale
                $max_width = $this->getWidth();
                $max_height =  $this->getHeight();      
        }

       	// Sortie du résultat
        header($this->getHeader());
		
        // Créer une ressource de travail
        $this->image_source = $this->getRessource();
		
        $this->image_copy = imagecreatetruecolor($max_width, $max_height);

        imagealphablending($this->image_copy, true);

        $colorToPaint = imagecolorallocatealpha($this->image_copy, 255, 0, 0, 75);
        imagefilledrectangle($this->image_copy, 0, 0, $max_width, $newHeight, $colorToPaint);

        imagecopyresampled(
                            $this->image_copy,
                            $this->image_source,
                            0,
                            0,
                            0,
                            $reflectedPart,
                            $max_width,
                            $reflectionHeight,
                            $max_width,
                            ($max_height - $reflectedPart));
							
        $this->imageFlipVertical();

        imagecopy($this->image_copy, $this->image_source, 0, 0, 0, 0, $max_width, $max_height);

        imagealphablending($this->image_copy, true);

        for($i=0;$i<$reflectionHeight;$i++) {
            $colorToPaint = imagecolorallocatealpha($this->image_copy,255, 255, 255, ($i/$reflectionHeight*-1+1)*$white);
            imagefilledrectangle($this->image_copy, 0, $max_height+$i, $max_width, $max_height+$i, $colorToPaint);
        }

        if($border == true) 
		{
            $rgb = $this->hex2rgb($borderColor, false);
            $colorToPaint = imagecolorallocate($this->image_copy, $rgb[0], $rgb[1], $rgb[2]);
            imageline($this->image_copy, 0, 0, $max_width, 0, $colorToPaint); //top line
            imageline($this->image_copy, 0, $max_height, $max_width, $max_height, $colorToPaint); //bottom line
            imageline($this->image_copy, 0, 0, 0, $max_height, $colorToPaint); //left line
            imageline($this->image_copy, $max_width-1, 0, $max_width-1, $max_height, $colorToPaint); //right line
        }
		
        switch($this->getType())
        {    
            case 1: 
				imagegif($this->image_copy, NULL); 
			break;
			
            case 2: 
				imagejpeg($this->image_copy, NULL, self::QUALITY_JPEG); 
			break;
			
            case 3: 
				imagepng($this->image_copy, NULL); 
			break;
        }		
	}
Je l'utilise comme ceci:
$thumb->reflection($type, 40, 40, 80, true, '#a4a4a4');
Merci d'avance...

Eléphant du PHP | 451 Messages

01 oct. 2008, 22:47

Je me permet un p'tit up (oulah je sens que les modérateurs vont pas aimer).

Mais voilà à default j'avais pris un script qui fait un reflet en javascript mais depuis que j'ai mit un autre script javascript le reflet ne fonctionne plus car les deux scripts utilise la balise rel.

Donc avec le reflet en php j'aurais plus aucun problème.

Est-ce que quelqu'un aurait une idée???

Thank you...

ViPHP
ViPHP | 4674 Messages

04 oct. 2008, 14:05

Hey :),

Uè, évite les ups hein.
Je n'ai rien lu à ton script, car c'est un peu lourd à lire … En revanche, l'idée est très simple.

Raisonne de façon algorithmique avant de te lancer dans le code, tu verras que ça ira mieux.
L'idée est donc la suivante : on récupère la hauteur de notre image, on crée une nouvelle image qui fait 2 fois cette hauteur (la largeur reste inchangée), et on place notre image en haut de la nouvelle. Ensuite on lit ligne par ligne notre nouvelle image jusqu'à arriver à la moitié. Pour chaque ligne lue, on la réécrit plus bas, exactement 2.h-i plus bas, où h est la hauteur de notre image originale, et i la ligne lue.
Là, tu auras seulement fait la symétrie de ton image. Il suffit alors de jouer sur la transparence de chaque ligne réécrite et le tour est joué.

Compris ?
« Un handicap est le résultat d'une rencontre entre une déficience ou différence et une incapacité de la société à répondre à celle-ci. »

Hoa : http://hoa-project.net (sur @hoaproject).

Eléphant du PHP | 451 Messages

05 oct. 2008, 21:26

Bonsoir,

Merci HyWan pour ta réponse.

Donc j'ai fait un code qui n'utilise pas ma classe.

Donc j'ai réussi à créer une image plus grande et à coller l'original en haut.

Voici mon code:
<?php

header ("Content-type: image/jpeg"); // L'image que l'on va créer est un jpeg

$destination = imagecreatefromjpeg("couchersoleil.jpg"); // La photo est la destination

$largeur_destination = imagesx($destination);
$hauteur_destination = imagesy($destination);

$image = imagecreatetruecolor($largeur_destination, ($hauteur_destination + ( $hauteur_destination / 2)));

imagecopyresampled($image, $destination, 0, 0, 0, 0, $largeur_destination, ($hauteur_destination + ( $hauteur_destination / 2)), $hauteur_destination, $largeur_destination);

imagejpeg($image);

?>
Mais là je bloque.

Je ne trouve pas la fonction, ni comment faire pour copier l'image pixel par pixel, Ni comment la retourner, ni pour la transparence.

ViPHP
ViPHP | 4674 Messages

05 oct. 2008, 23:47

Un seul lien : php.net/image
Tu devrais jeter un œil à imagecopy().
« Un handicap est le résultat d'une rencontre entre une déficience ou différence et une incapacité de la société à répondre à celle-ci. »

Hoa : http://hoa-project.net (sur @hoaproject).

Eléphant du PHP | 451 Messages

06 oct. 2008, 00:29

Merci HyWan j'y suis enfin arrivé.

Voici le code si cela peut servir à quelqu'un.
<?php

header ("Content-type: image/jpeg");

$filename = imagecreatefromjpeg("url_de_l'image");

$percent = 20;
$reflection = 20;
$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);
}

imagejpeg($new_img);

?>
Problème résolu merci ++