public function GDToolkit::createTmp

Creates a truecolor image preserving transparency from a provided image.

Parameters

object $image: An image object.

int $width: The new width of the new image, in pixels.

int $height: The new height of the new image, in pixels.

Return value

resource A GD image handle.

3 calls to GDToolkit::createTmp()
GDToolkit::crop in drupal/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
Implements \Drupal\system\Plugin\ImageToolkitInterface::crop().
GDToolkit::load in drupal/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
Implements \Drupal\system\Plugin\ImageToolkitInterface::load().
GDToolkit::resize in drupal/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
Implements \Drupal\system\Plugin\ImageToolkitInterface::resize().

File

drupal/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php, line 248
Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit;.

Class

GDToolkit
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\system\Plugin\ImageToolkit

Code

public function createTmp($image, $width, $height) {
  $res = imagecreatetruecolor($width, $height);
  if ($image->info['extension'] == 'gif') {

    // Grab transparent color index from image resource.
    $transparent = imagecolortransparent($image->resource);
    if ($transparent >= 0) {

      // The original must have a transparent color, allocate to the new image.
      $transparent_color = imagecolorsforindex($image->resource, $transparent);
      $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);

      // Flood with our new transparent color.
      imagefill($res, 0, 0, $transparent);
      imagecolortransparent($res, $transparent);
    }
  }
  elseif ($image->info['extension'] == 'png') {
    imagealphablending($res, FALSE);
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
    imagefill($res, 0, 0, $transparency);
    imagealphablending($res, TRUE);
    imagesavealpha($res, TRUE);
  }
  else {
    imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
  }
  return $res;
}