imagecolorsforindex example

if ($transparent >= 0) {
      // Find out the number of colors in the image palette. It will be 0 for       // truecolor images.       $palette_size = imagecolorstotal($this->getResource());
      if ($palette_size == 0 || $transparent < $palette_size) {
        // Return the transparent color, either if it is a truecolor image         // or if the transparent color is part of the palette.         // Since the index of the transparent color is a property of the         // image rather than of the palette, it is possible that an image         // could be created with this index set outside the palette size.         // (see http://stackoverflow.com/a/3898007).         $rgb = imagecolorsforindex($this->getResource()$transparent);
        unset($rgb['alpha']);
        return Color::rgbToHex($rgb);
      }
    }
    return NULL;
  }

  /** * {@inheritdoc} */
  public function getWidth() {
    
    $arguments['background_idx'] = imagecolorallocatealpha($this->getToolkit()->getResource()$background['red']$background['green']$background['blue']$background['alpha']);

    if ($this->getToolkit()->getType() === IMAGETYPE_GIF) {
      // GIF does not work with a transparency channel, but can define 1 color       // in its palette to act as transparent.
      // Get the current transparent color, if any.       $gif_transparent_id = imagecolortransparent($this->getToolkit()->getResource());
      if ($gif_transparent_id !== -1) {
        // The gif already has a transparent color set: remember it to set it on         // the rotated image as well.         $arguments['gif_transparent_color'] = imagecolorsforindex($this->getToolkit()->getResource()$gif_transparent_id);

        if ($background['alpha'] >= 127) {
          // We want a transparent background: use the color already set to act           // as transparent, as background.           $arguments['background_idx'] = $gif_transparent_id;
        }
      }
      else {
        // The gif does not currently have a transparent color set.         if ($background['alpha'] >= 127) {
          // But as the background is transparent, it should get one.

  public function getPixelColor(ImageInterface $image, int $x, int $y): array {
    $toolkit = $image->getToolkit();
    $color_index = imagecolorat($toolkit->getResource()$x$y);

    $transparent_index = imagecolortransparent($toolkit->getResource());
    if ($color_index == $transparent_index) {
      return [0, 0, 0, 127];
    }

    return array_values(imagecolorsforindex($toolkit->getResource()$color_index));
  }

  /** * Data provider for ::testManipulations(). */
  public function providerTestImageFiles(): array {
    // Typically the corner colors will be unchanged. These colors are in the     // order of top-left, top-right, bottom-right, bottom-left.     $default_corners = [static::RED, static::GREEN, static::BLUE, static::TRANSPARENT];

    // Setup a list of tests to perform on each type.
Home | Imprint | This part of the site doesn't use cookies.