Project

General

Profile

Revision b0dc3a2e

Added by Julien Enselme over 7 years ago

Update to Drupal 7.52

View differences:

drupal7/modules/system/image.gd.inc
116 116
    return FALSE;
117 117
  }
118 118

  
119
  $width = $image->info['width'];
120
  $height = $image->info['height'];
119
  // PHP 5.5 GD bug: https://bugs.php.net/bug.php?id=65148: To prevent buggy
120
  // behavior on negative multiples of 90 degrees we convert any negative
121
  // angle to a positive one between 0 and 360 degrees.
122
  $degrees -= floor($degrees / 360) * 360;
121 123

  
122
  // Convert the hexadecimal background value to a color index value.
124
  // Convert the hexadecimal background value to a RGBA array.
123 125
  if (isset($background)) {
124
    $rgb = array();
125
    for ($i = 16; $i >= 0; $i -= 8) {
126
      $rgb[] = (($background >> $i) & 0xFF);
127
    }
128
    $background = imagecolorallocatealpha($image->resource, $rgb[0], $rgb[1], $rgb[2], 0);
126
    $background = array(
127
      'red' => $background >> 16 & 0xFF,
128
      'green' => $background >> 8 & 0xFF,
129
      'blue' => $background & 0xFF,
130
      'alpha' => 0,
131
    );
129 132
  }
130
  // Set the background color as transparent if $background is NULL.
131 133
  else {
132
    // Get the current transparent color.
133
    $background = imagecolortransparent($image->resource);
134

  
135
    // If no transparent colors, use white.
136
    if ($background == 0) {
137
      $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);
138
    }
134
    // Background color is not specified: use transparent white as background.
135
    $background = array(
136
      'red' => 255,
137
      'green' => 255,
138
      'blue' => 255,
139
      'alpha' => 127
140
    );
139 141
  }
140 142

  
143
  // Store the color index for the background as that is what GD uses.
144
  $background_idx = imagecolorallocatealpha($image->resource, $background['red'], $background['green'], $background['blue'], $background['alpha']);
145

  
141 146
  // Images are assigned a new color palette when rotating, removing any
142 147
  // transparency flags. For GIF images, keep a record of the transparent color.
143 148
  if ($image->info['extension'] == 'gif') {
144
    $transparent_index = imagecolortransparent($image->resource);
145
    if ($transparent_index != 0) {
146
      $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
149
    // GIF does not work with a transparency channel, but can define 1 color
150
    // in its palette to act as transparent.
151

  
152
    // Get the current transparent color, if any.
153
    $gif_transparent_id = imagecolortransparent($image->resource);
154
    if ($gif_transparent_id !== -1) {
155
      // The gif already has a transparent color set: remember it to set it on
156
      // the rotated image as well.
157
      $transparent_gif_color = imagecolorsforindex($image->resource, $gif_transparent_id);
158

  
159
      if ($background['alpha'] >= 127) {
160
        // We want a transparent background: use the color already set to act
161
        // as transparent, as background.
162
        $background_idx = $gif_transparent_id;
163
      }
164
    }
165
    else {
166
      // The gif does not currently have a transparent color set.
167
      if ($background['alpha'] >= 127) {
168
        // But as the background is transparent, it should get one.
169
        $transparent_gif_color = $background;
170
      }
147 171
    }
148 172
  }
149 173

  
150
  $image->resource = imagerotate($image->resource, 360 - $degrees, $background);
174
  $image->resource = imagerotate($image->resource, 360 - $degrees, $background_idx);
151 175

  
152 176
  // GIFs need to reassign the transparent color after performing the rotate.
153 177
  if (isset($transparent_gif_color)) {

Also available in: Unified diff