snike | Bonjour,
Je cherche à générer des images via GD ç partir des filtres css du style :
Code :
- filter: sepia(0.5) hue-rotate(-30deg) saturate(1.2) contrast(0.8); // ig-1977
|
Sauf erreur de ma part, il n'y a pas directement dans GD de filtre sepia, hue-rotate et saturate, j'ai donc trouvé des équivalences mais le résultat n'est pas le même.
Code :
- $nom_fichier='test.JPG';
- $image = @imagecreatefromjpeg($nom_fichier);
- sepia($image,0.5); // sepia(0.5)
- imagehue($image, -30); // hue-rotate(-30deg)
- imageSaturation($image, 20); // saturate(1.2)
- imagefilter($image,IMG_FILTER_CONTRAST,190); //contrast(0.8)
- imagejpeg($image,"toto.jpg" );
- imagedestroy($image);
- // filter: sepia(0.5) hue-rotate(-30deg) saturate(1.2) contrast(0.8);
- // Filtre Sépia
- // Source : http://php.net/manual/fr/function.imagefilter.php
- function sepia(&$image,$p) {
- imagefilter($image,IMG_FILTER_GRAYSCALE,255*$p);
- imagefilter($image,IMG_FILTER_BRIGHTNESS,(-30/255)*$p);
- imagefilter($image,IMG_FILTER_COLORIZE, (90/255)*$p, (55/255)*$p, (30/255)*$p);
- }
- // Filtre hue-rotate
- // Source : https://stackoverflow.com/questions [...] gd-library
- function imagehue(&$image, $angle) {
- if ($angle % 360 == 0) return;
- $width = imagesx($image);
- $height = imagesy($image);
- for($x = 0; $x < $width; $x++) {
- for($y = 0; $y < $height; $y++) {
- $rgb = imagecolorat($image, $x, $y);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $alpha = ($rgb & 0x7F000000) >> 24;
- list($h, $s, $l) = rgb2hsl($r, $g, $b);
- $h += $angle / 360;
- if($h > 1) $h--;
- list($r, $g, $b) = hsl2rgb($h, $s, $l);
- imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
- }
- }
- }
- // Filtre saturate
- function imageSaturation(&$image, $saturationPercentage) {
- $width = imagesx($image);
- $height = imagesy($image);
- for($x = 0; $x < $width; $x++) {
- for($y = 0; $y < $height; $y++) {
- $rgb = imagecolorat($image, $x, $y);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $alpha = ($rgb & 0x7F000000) >> 24;
- list($h, $s, $l) = rgb2hsl($r, $g, $b);
- $s = $s * (100 + $saturationPercentage ) /100;
- if($s > 1) $s = 1;
- list($r, $g, $b) = hsl2rgb($h, $s, $l);
- imagesetpixel($image, $x, $y, imagecolorallocatealpha($image, $r, $g, $b, $alpha));
- }
- }
- }
|
Avez-vous des meilleurs scripts ?
Merci par avance.
|