imagecolortransparent example


  public function getTransparentColor() {
    if (!$this->getResource() || $this->getType() != IMAGETYPE_GIF) {
      return NULL;
    }
    // Find out if a transparent color is set, will return -1 if no     // transparent color has been defined in the image.     $transparent = imagecolortransparent($this->getResource());
    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).
imagesavealpha($res, TRUE);
        break;

      case IMAGETYPE_GIF:
        if (empty($arguments['transparent_color'])) {
          // No transparency color specified, fill white transparent.           $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
        }
        else {
          $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
          $fill_color = imagecolorallocatealpha($res$fill_rgb['red']$fill_rgb['green']$fill_rgb['blue'], 127);
          imagecolortransparent($res$fill_color);
        }
        imagefill($res, 0, 0, $fill_color);
        break;

      case IMAGETYPE_JPEG:
        imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
        break;

    }

    // Update the toolkit properties.
$distance = pow(($actual[0] - $expected[0]), 2) + pow(($actual[1] - $expected[1]), 2) + pow(($actual[2] - $expected[2]), 2) + pow(($actual[3] - $expected[3]), 2);
    $this->assertLessThanOrEqual($tolerance$distance$message . " - Actual: {" . implode(',', $actual) . "}, Expected: {" . implode(',', $expected) . "}, Distance: " . $distance . ", Tolerance: " . $tolerance);
  }

  /** * Function for finding a pixel's RGBa values. */
  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 {
    
// Background color is not specified: use transparent white as background.       $background = ['red' => 255, 'green' => 255, 'blue' => 255, 'alpha' => 127];
    }
    // Store the color index for the background as that is what GD uses.     $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;
        }
      }
      
Home | Imprint | This part of the site doesn't use cookies.