ToolkitGdTest.php 10.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\system\Tests\Image\ToolkitGdTest.
6 7
 */

8
namespace Drupal\system\Tests\Image;
9

10 11
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\system\Plugin\ImageToolkitManager;
12 13 14 15

/**
 * Test the core GD image manipulation functions.
 */
16
class ToolkitGdTest extends DrupalUnitTestBase {
17 18 19 20 21 22 23 24 25 26 27 28 29
  // Colors that are used in testing.
  protected $black       = array(0, 0, 0, 0);
  protected $red         = array(255, 0, 0, 0);
  protected $green       = array(0, 255, 0, 0);
  protected $blue        = array(0, 0, 255, 0);
  protected $yellow      = array(255, 255, 0, 0);
  protected $fuchsia     = array(255, 0, 255, 0); // Used as background colors.
  protected $transparent = array(0, 0, 0, 127);
  protected $white       = array(255, 255, 255, 0);

  protected $width = 40;
  protected $height = 20;

30 31 32 33 34 35 36
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system', 'simpletest');

37
  public static function getInfo() {
38
    return array(
39 40 41
      'name' => 'Image GD manipulation tests',
      'description' => 'Check that core image manipulations work properly: scale, resize, rotate, crop, scale and crop, and desaturate.',
      'group' => 'Image',
42 43 44
    );
  }

45
  protected function checkRequirements() {
46 47 48 49 50 51 52 53
    $gd_available = FALSE;
    if ($check = get_extension_funcs('gd')) {
      if (in_array('imagegd2', $check)) {
        // GD2 support is available.
        $gd_available =  TRUE;
      }
    }
    if (!$gd_available) {
54 55 56 57 58 59 60
      return array(
        'Image manipulations for the GD toolkit cannot run because the GD toolkit is not available.',
      );
    }
    return parent::checkRequirements();
  }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  /**
   * Function to compare two colors by RGBa.
   */
  function colorsAreEqual($color_a, $color_b) {
    // Fully transparent pixels are equal, regardless of RGB.
    if ($color_a[3] == 127 && $color_b[3] == 127) {
      return TRUE;
    }

    foreach ($color_a as $key => $value) {
      if ($color_b[$key] != $value) {
        return FALSE;
      }
    }

    return TRUE;
  }

  /**
   * Function for finding a pixel's RGBa values.
   */
  function getPixelColor($image, $x, $y) {
    $color_index = imagecolorat($image->resource, $x, $y);

    $transparent_index = imagecolortransparent($image->resource);
    if ($color_index == $transparent_index) {
      return array(0, 0, 0, 127);
    }

    return array_values(imagecolorsforindex($image->resource, $color_index));
  }

  /**
   * Since PHP can't visually check that our images have been manipulated
   * properly, build a list of expected color values for each of the corners and
   * the expected height and widths for the final images.
   */
  function testManipulations() {
    // Typically the corner colors will be unchanged. These colors are in the
    // order of top-left, top-right, bottom-right, bottom-left.
    $default_corners = array($this->red, $this->green, $this->blue, $this->transparent);

    // A list of files that will be tested.
    $files = array(
      'image-test.png',
      'image-test.gif',
      'image-test.jpg',
    );

    // Setup a list of tests to perform on each type.
    $operations = array(
      'resize' => array(
        'function' => 'resize',
        'arguments' => array(20, 10),
        'width' => 20,
        'height' => 10,
        'corners' => $default_corners,
      ),
      'scale_x' => array(
        'function' => 'scale',
        'arguments' => array(20, NULL),
        'width' => 20,
        'height' => 10,
        'corners' => $default_corners,
      ),
      'scale_y' => array(
        'function' => 'scale',
        'arguments' => array(NULL, 10),
        'width' => 20,
        'height' => 10,
        'corners' => $default_corners,
      ),
      'upscale_x' => array(
        'function' => 'scale',
        'arguments' => array(80, NULL, TRUE),
        'width' => 80,
        'height' => 40,
        'corners' => $default_corners,
      ),
      'upscale_y' => array(
        'function' => 'scale',
        'arguments' => array(NULL, 40, TRUE),
        'width' => 80,
        'height' => 40,
        'corners' => $default_corners,
      ),
      'crop' => array(
        'function' => 'crop',
        'arguments' => array(12, 4, 16, 12),
        'width' => 16,
        'height' => 12,
        'corners' => array_fill(0, 4, $this->white),
      ),
      'scale_and_crop' => array(
        'function' => 'scale_and_crop',
        'arguments' => array(10, 8),
        'width' => 10,
        'height' => 8,
        'corners' => array_fill(0, 4, $this->black),
      ),
    );

163
    // Systems using non-bundled GD2 don't have imagerotate. Test if available.
164
    if (function_exists('imagerotate')) {
165 166 167 168 169 170 171 172 173 174 175 176 177
      $operations += array(
        'rotate_5' => array(
          'function' => 'rotate',
          'arguments' => array(5, 0xFF00FF), // Fuchsia background.
          'width' => 42,
          'height' => 24,
          'corners' => array_fill(0, 4, $this->fuchsia),
        ),
        'rotate_90' => array(
          'function' => 'rotate',
          'arguments' => array(90, 0xFF00FF), // Fuchsia background.
          'width' => 20,
          'height' => 40,
178
          'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        ),
        'rotate_transparent_5' => array(
          'function' => 'rotate',
          'arguments' => array(5),
          'width' => 42,
          'height' => 24,
          'corners' => array_fill(0, 4, $this->transparent),
        ),
        'rotate_transparent_90' => array(
          'function' => 'rotate',
          'arguments' => array(90),
          'width' => 20,
          'height' => 40,
          'corners' => array($this->transparent, $this->red, $this->green, $this->blue),
        ),
      );
    }

    // Systems using non-bundled GD2 don't have imagefilter. Test if available.
198
    if (function_exists('imagefilter')) {
199 200 201 202 203 204 205 206 207 208 209 210 211
      $operations += array(
        'desaturate' => array(
          'function' => 'desaturate',
          'arguments' => array(),
          'height' => 20,
          'width' => 40,
          // Grayscale corners are a bit funky. Each of the corners are a shade of
          // gray. The values of these were determined simply by looking at the
          // final image to see what desaturated colors end up being.
          'corners' => array(
            array_fill(0, 3, 76) + array(3 => 0),
            array_fill(0, 3, 149) + array(3 => 0),
            array_fill(0, 3, 29) + array(3 => 0),
212
            array_fill(0, 3, 225) + array(3 => 127)
213 214 215 216 217
          ),
        ),
      );
    }

218
    $manager = new ImageToolkitManager($this->container->getParameter('container.namespaces'));
219 220 221
    foreach ($files as $file) {
      foreach ($operations as $op => $values) {
        // Load up a fresh image.
222
        $image = image_load(drupal_get_path('module', 'simpletest') . '/files/' . $file, $manager->createInstance('gd'));
223
        if (!$image) {
224
          $this->fail(t('Could not load image %file.', array('%file' => $file)));
225 226 227
          continue 2;
        }

228 229
        // All images should be converted to truecolor when loaded.
        $image_truecolor = imageistruecolor($image->resource);
230
        $this->assertTrue($image_truecolor, format_string('Image %file after load is a truecolor image.', array('%file' => $file)));
231

232 233
        if ($image->info['extension'] == 'gif') {
          if ($op == 'desaturate') {
234 235
            // Transparent GIFs and the imagefilter function don't work together.
            $values['corners'][3][3] = 0;
236 237 238 239
          }
        }

        // Perform our operation.
240
        $function = 'image_' . $values['function'];
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        $arguments = array();
        $arguments[] = &$image;
        $arguments = array_merge($arguments, $values['arguments']);
        call_user_func_array($function, $arguments);

        // To keep from flooding the test with assert values, make a general
        // value for whether each group of values fail.
        $correct_dimensions_real = TRUE;
        $correct_dimensions_object = TRUE;
        $correct_colors = TRUE;

        // Check the real dimensions of the image first.
        if (imagesy($image->resource) != $values['height'] || imagesx($image->resource) != $values['width']) {
          $correct_dimensions_real = FALSE;
        }

        // Check that the image object has an accurate record of the dimensions.
        if ($image->info['width'] != $values['width'] || $image->info['height'] != $values['height']) {
          $correct_dimensions_object = FALSE;
        }

262
        $directory = $this->public_files_directory .'/imagetest';
263
        file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
264 265
        image_save($image, $directory . '/' . $op . '.' . $image->info['extension']);

266 267
        $this->assertTrue($correct_dimensions_real, format_string('Image %file after %action action has proper dimensions.', array('%file' => $file, '%action' => $op)));
        $this->assertTrue($correct_dimensions_object, format_string('Image %file object after %action action is reporting the proper height and width values.', array('%file' => $file, '%action' => $op)));
268

269 270
        // JPEG colors will always be messed up due to compression.
        if ($image->info['extension'] != 'jpg') {
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
          // Now check each of the corners to ensure color correctness.
          foreach ($values['corners'] as $key => $corner) {
            // Get the location of the corner.
            switch ($key) {
              case 0:
                $x = 0;
                $y = 0;
                break;
              case 1:
                $x = $values['width'] - 1;
                $y = 0;
                break;
              case 2:
                $x = $values['width'] - 1;
                $y = $values['height'] - 1;
                break;
              case 3:
                $x = 0;
                $y = $values['height'] - 1;
                break;
            }
            $color = $this->getPixelColor($image, $x, $y);
            $correct_colors = $this->colorsAreEqual($color, $corner);
294
            $this->assertTrue($correct_colors, format_string('Image %file object after %action action has the correct color placement at corner %corner.', array('%file' => $file, '%action' => $op, '%corner' => $key)));
295
          }
296 297 298 299 300 301
        }
      }
    }

  }
}