Показаны сообщения с ярлыком images. Показать все сообщения
Показаны сообщения с ярлыком images. Показать все сообщения

Kohana v2.3.4. Image resizing. Transparency losing

I've found interesting bug in Kohana's Image module: sometimes it losing transparency while resizing images. The point was that sometimes imagecopyresized was used.

Let's take a look at system/libraries/drivers/Image/GD.php, Image_GD_Driver#resize:

//...
// Test if we can do a resize without resampling to speed up the final resize
if (false && $properties['width'] > $width / 2 AND $properties['height'] > $height / 2)
{
  // Presize width and height
  $pre_width = $width;
  $pre_height = $height;

  // The maximum reduction is 10% greater than the final size
  $max_reduction_width  = round($properties['width']  * 1.1);
  $max_reduction_height = round($properties['height'] * 1.1);

  // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
  while ($pre_width / 2 > $max_reduction_width AND $pre_height / 2 > $max_reduction_height)
  {
    $pre_width /= 2;
    $pre_height /= 2;
  }

  // Create the temporary image to copy to
  $img = $this->imagecreatetransparent($pre_width, $pre_height);

  if ($status = imagecopyresized($img, $this->tmp_image, 0, 0, 0, 0, $pre_width, $pre_height,   $width, $height))
  {
    // Swap the new image for the old one
    imagedestroy($this->tmp_image);
    $this->tmp_image = $img;
  }

  // Set the width and height to the presize
  $width  = $pre_width;
  $height = $pre_height;
 }

   
We can see, that in case when old size of picture is larger than new more than 2 times, this method uses imagecopyresized to resize image to 110% of target size to make imagecopyresampled faster without quality losing. It's nice decision but it's not working for PNG!

To solve this problem in my project I've just copied GD.php from system/libraries/drivers/Image/GD.php to application/libraries/drivers/Image/GD.php and then removed this block from resize method.

Good luck!




2008–2014 © Сергей Удалов Реклама Система Orphus