/** * @param resource $resource * @param resource $controlResource * @return resource */ public function getOptimizedGdResource($resource, $controlResource) { if (imageistruecolor($resource)) { $resource = $this->rh->getPalettizedGdResource($resource); } $totalColors = imagecolorstotal($resource); $trans = imagecolortransparent($resource); $reds = new \SplFixedArray($totalColors); $greens = new \SplFixedArray($totalColors); $blues = new \SplFixedArray($totalColors); $i = 0; do { $colors = imagecolorsforindex($resource, $i); $reds[$i] = $colors['red']; $greens[$i] = $colors['green']; $blues[$i] = $colors['blue']; } while (++$i < $totalColors); if (imageistruecolor($controlResource)) { $controlResource = $this->rh->getPalettizedGdResource($controlResource); } $controlTotalColors = imagecolorstotal($controlResource); $controlTrans = imagecolortransparent($controlResource); $controlReds = new \SplFixedArray($controlTotalColors); $controlGreens = new \SplFixedArray($controlTotalColors); $controlBlues = new \SplFixedArray($controlTotalColors); $i = 0; do { $colors = imagecolorsforindex($controlResource, $i); $controlReds[$i] = $colors['red']; $controlGreens[$i] = $colors['green']; $controlBlues[$i] = $colors['blue']; } while (++$i < $controlTotalColors); $width = imagesx($resource); $height = imagesy($resource); $y = 0; do { $x = 0; do { $index = imagecolorat($resource, $x, $y); $red = $reds[$index]; $green = $greens[$index]; $blue = $blues[$index]; $controlIndex = imagecolorat($controlResource, $x, $y); $controlRed = $controlReds[$controlIndex]; $controlGreen = $controlGreens[$controlIndex]; $controlBlue = $controlBlues[$controlIndex]; if (($red & 0b11111100) === ($controlRed & 0b11111100) && ($green & 0b11111100) === ($controlGreen & 0b11111100) && ($blue & 0b11111100) === ($controlBlue & 0b11111100)) { imagesetpixel($resource, $x, $y, $trans); } } while (++$x !== $width); } while (++$y !== $height); return $resource; }
/** * @param LayerInterface[] $layers */ protected function initCacheResource(array $layers) { if (1 == count($layers)) { return; } foreach ($layers as $layer) { if ($layer instanceof BackgroundLayerInterface) { $width = $layer->get('final.width'); $height = $layer->get('final.height'); $resource = $this->rh->getEmptyGdResource($width, $height); $layer->set('gif.cache_resource', $resource); $backgroundLayer = $layer; continue; } if ($layer instanceof ImageAwareLayerInterface) { $this->initImageAwareLayerResource($layer); } elseif ($layer instanceof TextLayerInterface) { $this->initTextLayerResource($layer); } $resource = $this->rh->getMergedGdResource($backgroundLayer->get('gif.cache_resource'), $layer->get('final.resource'), $layer->get('regular.move.x'), $layer->get('regular.move.y'), $layer->get('regular.move.gravity')); if (!$backgroundLayer->get('gif.quality') && imageistruecolor($resource)) { $resource = $this->rh->getPalettizedGdResource($resource); } imagedestroy($layer->get('final.resource')); $backgroundLayer->set('gif.cache_resource', $resource); } }