Changeset 12749 for trunk/admin/include


Ignore:
Timestamp:
Dec 16, 2011, 6:00:06 PM (13 years ago)
Author:
patdenice
Message:

bug:2535
Image is not cropped and ressized if requested size is bigger than image size.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/image.class.php

    r12581 r12749  
    9898    $source_height = $this->image->get_height();
    9999
    100     // Crop image
    101     if ($crop)
    102     {
    103       $x = 0;
    104       $y = 0;
    105 
    106       if ($source_width < $source_height and $follow_orientation)
    107       {
    108         list($max_width, $max_height) = array($max_height, $max_width);
    109       }
    110 
    111       $img_ratio = $source_width / $source_height;
    112       $dest_ratio = $max_width / $max_height;
    113 
    114       if($dest_ratio > $img_ratio)
    115       {
    116         $destHeight = round($source_width * $max_height / $max_width);
    117         $y = round(($source_height - $destHeight) / 2 );
    118         $source_height = $destHeight;
    119       }
    120       elseif ($dest_ratio < $img_ratio)
    121       {
    122         $destWidth = round($source_height * $max_width / $max_height);
    123         $x = round(($source_width - $destWidth) / 2 );
    124         $source_width = $destWidth;
    125       }
    126 
    127       $this->image->crop($source_width, $source_height, $x, $y);
    128     }
    129 
    130100    $rotation = null;
    131101    if ($automatic_rotation)
     
    133103      $rotation = self::get_rotation_angle($this->source_filepath);
    134104    }
    135     $resize_dimensions = self::get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
     105    $resize_dimensions = self::get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation, $crop, $follow_orientation);
    136106
    137107    // testing on height is useless in theory: if width is unchanged, there
     
    151121      $this->image->strip();
    152122    }
     123
     124    if (isset($resize_dimensions['crop']))
     125    {
     126      $this->image->crop($resize_dimensions['crop']['width'], $resize_dimensions['crop']['height'], $resize_dimensions['crop']['x'], $resize_dimensions['crop']['y']);
     127    }
    153128   
    154129    $this->image->resize($resize_dimensions['width'], $resize_dimensions['height']);
     
    165140  }
    166141
    167   static function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null)
     142  static function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null, $crop=false, $follow_orientation=true)
    168143  {
    169144    $rotate_for_dimensions = false;
     
    176151    {
    177152      list($width, $height) = array($height, $width);
     153    }
     154
     155    if ($crop)
     156    {
     157      $x = 0;
     158      $y = 0;
     159
     160      if ($width < $height and $follow_orientation)
     161      {
     162        list($max_width, $max_height) = array($max_height, $max_width);
     163      }
     164
     165      $img_ratio = $width / $height;
     166      $dest_ratio = $max_width / $max_height;
     167
     168      if($dest_ratio > $img_ratio)
     169      {
     170        $destHeight = round($width * $max_height / $max_width);
     171        $y = round(($height - $destHeight) / 2 );
     172        $height = $destHeight;
     173      }
     174      elseif ($dest_ratio < $img_ratio)
     175      {
     176        $destWidth = round($height * $max_width / $max_height);
     177        $x = round(($width - $destWidth) / 2 );
     178        $width = $destWidth;
     179      }
    178180    }
    179181   
     
    203205    }
    204206
    205     return array(
     207    $result = array(
    206208      'width' => $destination_width,
    207209      'height'=> $destination_height,
    208210      );
     211
     212    if ($crop and ($x or $y))
     213    {
     214      $result['crop'] = array(
     215        'width' => $width,
     216        'height' => $height,
     217        'x' => $x,
     218        'y' => $y,
     219        );
     220    }
     221    return $result;
    209222  }
    210223
Note: See TracChangeset for help on using the changeset viewer.