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...