public function ImageTrueColorToPalette2(&$image, $dither, $ncolors) { // http://www.php.net/manual/en/function.imagetruecolortopalette.php // zmorris at zsculpt dot com (17-Aug-2004 06:58) $width = ImageSX($image); $height = ImageSY($image); $image_copy = ImageCreateTrueColor($width, $height); //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100); ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height); ImageTrueColorToPalette($image, $dither, $ncolors); ImageColorMatch($image_copy, $image); ImageDestroy($image_copy); return true; }
function colorDecrease($image, $dither, $ncolors) { $width = imagesx($image); $height = imagesy($image); $colors_handle = ImageCreateTrueColor($width, $height); ImageCopyMerge($colors_handle, $image, 0, 0, 0, 0, $width, $height, 100); ImageTrueColorToPalette($image, $dither, $ncolors); ImageColorMatch($colors_handle, $image); ImageDestroy($colors_handle); }
function ImageTrueColorToPalette2($original_image, $dither, $ncolors) { $width = imagesx($original_image); $height = imagesy($original_image); $colors_handle = ImageCreateTrueColor($width, $height); imagecopymerge($colors_handle, $original_image, 0, 0, 0, 0, $width, $height, 100); ImageTrueColorToPalette($original_image, $dither, $ncolors); ImageColorMatch($colors_handle, $original_image); ImageDestroy($colors_handle); return $original_image; }
private function retailler($nouvelle_largeur, $nouvelle_hauteur, $delta_largeur, $delta_hauteur, $reduite = false) { $ret = false; if (!$this->is_null()) { $src_r = null; if (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_JPG) || !strcmp($this->get_ext(), _UPLOAD_EXTENSION_JPEG)) { $src_r = imagecreatefromjpeg($this->get_src()); } elseif (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_PNG)) { $src_r = imagecreatefrompng($this->get_src()); } elseif (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_GIF)) { $src_r = imagecreatefromgif($this->get_src()); } if ($src_r) { if ($this->get_ext() == _UPLOAD_EXTENSION_JPG || $this->get_ext() == _UPLOAD_EXTENSION_JPEG) { $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); $qualite = $reduite ? self::qualite_jpg_reduite : self::qualite_jpg; $ret = imagejpeg($dst_r, $this->get_src(), $qualite); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } elseif ($this->get_ext() == _UPLOAD_EXTENSION_PNG) { $src_alpha = $this->png_has_transparency($this->get_src()); $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { if ($src_alpha) { imagealphablending($dst_r, false); imagesavealpha($dst_r, true); } imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); /* En cas d'image non transparente on reduit à une image avec palette (pb de taille) */ if (!$src_alpha) { $tmp = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); ImageCopyMerge($tmp, $dst_r, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur, 100); ImageTrueColorToPalette($dst_r, false, 8192); ImageColorMatch($tmp, $dst_r); ImageDestroy($tmp); } $qualite = $reduite ? self::qualite_png_reduite : self::qualite_png; $ret = imagepng($dst_r, $this->get_src(), $qualite); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } elseif ($this->get_ext() == _UPLOAD_EXTENSION_GIF) { $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); $ret = imagegif($dst_r, $this->get_src()); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } imagedestroy($src_r); } } return $ret; }
private function colorDecrease() { $gifFInal = TMPDIR . "/" . $this->outWidth . "x" . $this->outHeight . "_T.gif"; if ($this->inFormat == "image/jpeg") { $src_img = ImageCreateFromJPEG($this->inPath) or die('colorDecrease: Problema leyendo jpg original'); } else { $src_img = ImageCreateFromGIF($this->inPath) or die('colorDecrease: Problema leyendo gif original'); } $dest_img = imagecreatetruecolor($this->outWidth, $this->outHeight) or die('colorDecrease: Problema en la creaci�n de la imgen nueva gif'); imagecopy($dest_img, $src_img, 0, 0, 0, 0, $this->outWidth, $this->outHeight); ImageTrueColorToPalette($dest_img, true, $this->nColors); ImageColorMatch($src_img, $dest_img); if (!imagegif($dest_img, $gifFInal)) { die("colorDecrease: Problema guardando la IMG gif en: {$outPath}"); } ImageDestroy($dest_img); $this->actualSize = filesize($gifFInal); if ($this->actualSize > $this->maxFileSize) { $this->nColors--; if (unlink($gifFInal)) { $this->colorDecrease(); } } else { unlink($this->jpgGifXcolors); } }