/** * @param Frame $frame * @param $index * @return resource */ protected function render(Frame $frame, $index) { if ($index == 0) { $screenSize = $this->decoder->getScreenSize(); $im = imagecreatetruecolor($screenSize->getWidth(), $screenSize->getHeight()); imagealphablending($im, false); imagesavealpha($im, true); $transColor = imagecolortransparent($im, imagecolorallocatealpha($im, 255, 255, 255, 127)); imagefill($im, 0, 0, $transColor); $this->frameCurrent = $im; $this->framePrevious = $frame; $this->copyFrameToBuffer($frame); return $this->frameCurrent; } imagepalettetotruecolor($this->frameCurrent); $disposalMethod = $this->framePrevious->getDisposalMethod(); if ($disposalMethod === 0 || $disposalMethod === 1) { $this->copyFrameToBuffer($frame); } elseif ($disposalMethod === 2) { $this->restoreToBackground($this->framePrevious, imagecolortransparent($this->frameCurrent)); $this->copyFrameToBuffer($frame); } else { throw new \RuntimeException("Disposal method {$disposalMethod} is not implemented."); } $this->framePrevious = $frame; return $this->frameCurrent; }
/** * Convert the image to 2 colours with dithering. */ protected function dither() { if (!imageistruecolor($this->image)) { imagepalettetotruecolor($this->image); } imagefilter($this->image, IMG_FILTER_GRAYSCALE); imagetruecolortopalette($this->image, true, 2); }
/** * @param resource $bitmap Image DG resource. It becomes this object own. So the resource can be modified and it * will be automatically destroyed on this object destruction. * @param bool $isTransparent Whether image resource has transparent pixels. Not significant argument, it's used * only when output file format is selected. * @throws \InvalidArgumentException If given resource is not a DG resource * @throws \Exception */ public function __construct($bitmap, $isTransparent = false) { if (!is_resource($bitmap)) { throw new \InvalidArgumentException('Argument $bitmap expected to be resource, ' . gettype($bitmap) . 'given.'); } if (get_resource_type($bitmap) !== 'gd') { throw new \InvalidArgumentException('The resource from the $bitmap argument is not image resource.'); } if (!imageistruecolor($bitmap)) { if (!@imagepalettetotruecolor($bitmap)) { throw new \Exception('Can\'t transform image to True Color. Perhaps not enough RAM.'); } } @imagealphablending($bitmap, true); $this->bitmap = $bitmap; $this->isTransparent = !!$isTransparent; }
public function __construct($imagePath, $map = self::WHITE_MAP) { if (!file_exists($imagePath)) { throw new Exception("Path was not found."); } //var_dump(realpath($imagePath));exit; $this->name = basename(realpath($imagePath)); $this->x = 0; $this->y = 0; $imageInfo = getimagesize($imagePath); $this->w = $imageInfo[0]; $this->h = $imageInfo[1]; $this->path = $imagePath; $this->colorMap = $map === self::BLACK_MAP ? self::$BLACK_COLOR_MAP : self::$WHITE_COLOR_MAP; $this->image = imagecreatefromstring(file_get_contents($imagePath)); $this->is32Bit = imageistruecolor($this->image); imagepalettetotruecolor($this->image); imagealphablending($this->image, false); imagesavealpha($this->image, true); }
function create_image() { $info = getimagesize($this->file_name); switch ($info[2]) { case IMAGETYPE_GIF: if (($this->image = imagecreatefromgif($this->file_name)) !== false) { imagepalettetotruecolor($this->image); } break; case IMAGETYPE_JPEG: if (function_exists('imagecreatefromjpeg')) { $this->image = imagecreatefromjpeg($this->file_name); return; } break; case IMAGETYPE_PNG: $this->image = imagecreatefrompng($this->file_name); break; } return; }
/** * Converts the image to true color */ protected function convertToTrueColor() { if (!imageistruecolor($this->resource)) { if (function_exists('imagepalettetotruecolor')) { // Available in PHP 5.5 imagepalettetotruecolor($this->resource); } else { $transparentIndex = imagecolortransparent($this->resource); $w = $this->width(); $h = $this->height(); $img = imagecreatetruecolor($w, $h); imagecopy($img, $this->resource, 0, 0, 0, 0, $w, $h); if ($transparentIndex != -1) { $width = $this->width(); $height = $this->height(); imagealphablending($img, false); imagesavealpha($img, true); for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { if (imagecolorat($this->resource, $x, $y) == $transparentIndex) { imagesetpixel($img, $x, $y, 127 << 24); } } } } $this->resource = $img; } } imagesavealpha($this->resource, true); }
/** * Transforme la couleur de fond de l'image en transparence * Le filtre ne gere pas la notion de contiguite aux bords, et affectera tous les pixels de l'image dans la couleur visee * $background_color : couleur cible * $tolerance : distance L1 dans l'espace RGB des couleur autour de la couleur $background_color pour lequel la transparence sera appliquee * $alpha : transparence a appliquer pour les pixels de la couleur cibles avec la tolerance ci-dessus * $coeff_lissage : coeff applique a la tolerance pour determiner la decroissance de la transparence fonction de la distance L1 entre la couleur du pixel et la couleur cible * * @param string $im * @param string $background_color * @param int $tolerance * @param int $alpha * alpha = 0: aucune transparence * alpha = 127: completement transparent * @param int $coeff_lissage * @return mixed|null|string */ function image_fond_transparent($im, $background_color, $tolerance = 12, $alpha = 127, $coeff_lissage = 7) { $fonction = array('image_fond_transparent', func_get_args()); $image = _image_valeurs_trans($im, "fond_transparent-{$background_color}-{$tolerance}-{$coeff_lissage}-{$alpha}", "png", $fonction); if (!$image) { return ""; } $x_i = $image["largeur"]; $y_i = $image["hauteur"]; $im = $image["fichier"]; $dest = $image["fichier_dest"]; $creer = $image["creer"]; if ($creer) { $bg = _couleur_hex_to_dec($background_color); $bg_r = $bg['red']; $bg_g = $bg['green']; $bg_b = $bg['blue']; // Creation de l'image en deux temps // de facon a conserver les GIF transparents $im = $image["fonction_imagecreatefrom"]($im); imagepalettetotruecolor($im); $im2 = imagecreatetruecolor($x_i, $y_i); @imagealphablending($im2, false); @imagesavealpha($im2, true); $color_t = ImageColorAllocateAlpha($im2, 255, 255, 255, 127); imagefill($im2, 0, 0, $color_t); imagecopy($im2, $im, 0, 0, 0, 0, $x_i, $y_i); $im_ = imagecreatetruecolor($x_i, $y_i); imagealphablending($im_, false); imagesavealpha($im_, true); $color_f = ImageColorAllocateAlpha($im_, 255, 255, 255, $alpha); for ($x = 0; $x < $x_i; $x++) { for ($y = 0; $y < $y_i; $y++) { $rgb = ImageColorAt($im2, $x, $y); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; if (($d = abs($r - $bg_r) + abs($g - $bg_g) + abs($b - $bg_b)) <= $tolerance) { imagesetpixel($im_, $x, $y, $color_f); } elseif ($tolerance and $d <= ($coeff_lissage + 1) * $tolerance) { $transp = round($alpha * (1 - ($d - $tolerance) / ($coeff_lissage * $tolerance))); $color_p = ImageColorAllocateAlpha($im_, $r, $g, $b, $transp); imagesetpixel($im_, $x, $y, $color_p); } else { imagesetpixel($im_, $x, $y, $rgb); } } } _image_gd_output($im_, $image); imagedestroy($im_); imagedestroy($im); imagedestroy($im2); } return _image_ecrire_tag($image, array('src' => $dest)); }
function _image_couleur_extraire($img, $x = 10, $y = 6) { static $couleur_extraite = array(); if (isset($couleur_extraite["{$img}-{$x}-{$y}"])) { return $couleur_extraite["{$img}-{$x}-{$y}"]; } // valeur par defaut si l'image ne peut etre lue $defaut = "F26C4E"; $cache = _image_valeurs_trans($img, "coul-{$x}-{$y}", "txt"); if (!$cache) { return $couleur_extraite["{$img}-{$x}-{$y}"] = $defaut; } $fichier = $cache["fichier"]; $dest = $cache["fichier_dest"]; if (isset($couleur_extraite["{$fichier}-{$x}-{$y}"])) { return $couleur_extraite["{$fichier}-{$x}-{$y}"]; } $creer = $cache["creer"]; if ($creer) { if (@file_exists($fichier)) { $width = $cache["largeur"]; $height = $cache["hauteur"]; $newwidth = 20; $newheight = 20; $thumb = imagecreate($newwidth, $newheight); $source = $cache["fonction_imagecreatefrom"]($fichier); imagepalettetotruecolor($source); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); do { // get a color $color_index = imagecolorat($thumb, $x, $y); // make it human readable $color_tran = imagecolorsforindex($thumb, $color_index); $x++; $y++; } while ($color_tran['alpha'] == 127 and $x < $newwidth and $y < $newheight); $couleur = _couleur_dec_to_hex($color_tran["red"], $color_tran["green"], $color_tran["blue"]); } else { $couleur = $defaut; } // Mettre en cache le resultat $couleur_extraite["{$fichier}-{$x}-{$y}"] = $couleur; ecrire_fichier($dest, $couleur_extraite["{$fichier}-{$x}-{$y}"]); } else { lire_fichier($dest, $couleur_extraite["{$fichier}-{$x}-{$y}"]); } return $couleur_extraite["{$img}-{$x}-{$y}"] = $couleur_extraite["{$fichier}-{$x}-{$y}"]; }
<?php imagepalettetotruecolor("bla");
<?php imagepalettetotruecolor();
<?php $im = imagecreate(100, 100); var_dump(is_resource($im)); var_dump(imageistruecolor($im)); var_dump(imagepalettetotruecolor($im)); var_dump(imageistruecolor($im)); imagedestroy($im);
<?php $im = fopen('php://memory', 'w'); imagepalettetotruecolor($im);
/** * Create a new GD image from the supplied string. * * @param $data * @return resource */ protected function create($data) { if (false === ($image = imagecreatefromstring($data))) { throw new RuntimeException('Could not read image.'); } if (!imageistruecolor($image)) { imagepalettetotruecolor($image); } imagefilter($image, IMG_FILTER_GRAYSCALE); return $image; }