Ignore:
Timestamp:
Mar 19, 2011, 1:24:41 PM (14 years ago)
Author:
patdenice
Message:

User can define personalized ration.

Location:
extensions/square_thumbnails/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/square_thumbnails/trunk/functions.inc.php

    r9757 r9770  
    4343    $srcWidth    = imagesx( $srcImage );
    4444    $srcHeight   = imagesy( $srcImage );
    45     $x = 0;
    46     $y = 0;
    47 
    48     if($srcWidth > $srcHeight)
    49     {
    50       $x = ceil(($srcWidth - $srcHeight) / 2 );
    51       $srcWidth = $srcHeight;
    52     }
    53     elseif ($srcHeight > $srcWidth)
    54     {
    55       $y = ceil(($srcHeight - $srcWidth) / 2);
    56       $srcHeight = $srcWidth;
    57     }
     45
     46    $coord = process_ratio($srcWidth, $srcHeight);
    5847
    5948    $ratioWidth  = $srcWidth/$newWidth;
     
    8574      // GD 2.0 or more recent -> good results (but slower)
    8675      $destImage = imagecreatetruecolor( $destWidth, $destHeight);
    87       imagecopyresampled( $destImage, $srcImage, 0, 0, $x, $y,
     76      imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'],
    8877                          $destWidth,$destHeight,$srcWidth,$srcHeight );
    8978    }
     
    9281      // GD prior to version  2 -> pretty bad results :-/ (but fast)
    9382      $destImage = imagecreate( $destWidth, $destHeight);
    94       imagecopyresized( $destImage, $srcImage, 0, 0, $x, $y,
     83      imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'],
    9584                        $destWidth,$destHeight,$srcWidth,$srcHeight );
    9685    }
     
    195184  $source_width  = imagesx($source_image);
    196185  $source_height = imagesy($source_image);
    197   $x = 0;
    198   $y = 0;
    199 
    200   if($source_width > $source_height)
    201   {
    202     $x = ceil(($source_width - $source_height) / 2 );
    203     $source_width = $source_height;
    204   }
    205   elseif ($source_height > $source_width)
    206   {
    207     $y = ceil(($source_height - $source_width) / 2);
    208     $source_height = $source_width;
    209   }
    210  
     186 
     187  $coord = process_ratio($source_width, $source_height);
     188
    211189  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
    212190
     
    227205    0,
    228206    0,
    229     $x,
    230     $y,
     207    $coord['x'],
     208    $coord['y'],
    231209    $resize_dimensions['width'],
    232210    $resize_dimensions['height'],
     
    275253  $source_height = $image->getImageHeight();
    276254
    277   // Crop image -> square
    278   $x = 0;
    279   $y = 0;
    280   if($source_width > $source_height)
    281   {
    282     $x = ceil(($source_width - $source_height) / 2 );
    283     $source_width = $source_height;
    284   }
    285   elseif ($source_height > $source_width)
    286   {
    287     $y = ceil(($source_height - $source_width) / 2);
    288     $source_height = $source_width;
    289   }
    290   $image->cropImage($source_width, $source_height, $x, $y);
     255  $coord = process_ratio($source_width, $source_height);
     256
     257  $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']);
    291258 
    292259  $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation);
     
    326293}
    327294
     295function process_ratio(&$source_width, &$source_height)
     296{
     297  global $conf;
     298
     299  if (!isset($conf['thumbnails_ratio']) or !preg_match('/^\d+:\d+$/', $conf['thumbnails_ratio']))
     300  {
     301    $conf['thumbnails_ratio'] = '1:1';
     302  }
     303
     304  $x = 0;
     305  $y = 0;
     306
     307  if ($source_width >= $source_height)
     308    list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']);
     309  else
     310    list($heightRatio, $widthRatio) = explode(':', $conf['thumbnails_ratio']);
     311
     312  $img_ratio = $source_width / $source_height;
     313  $dest_ratio = $widthRatio / $heightRatio;
     314
     315  if($dest_ratio > $img_ratio)
     316  {
     317    $destHeight = ceil(($source_width * $heightRatio) / $widthRatio);
     318    $y = ceil(($source_height - $destHeight) / 2 );
     319    $source_height = $destHeight ;
     320  }
     321  elseif ($dest_ratio < $img_ratio)
     322  {
     323    $destWidth = ceil(($source_height * $widthRatio) / $heightRatio);
     324    $x = ceil(($source_width - $destWidth) / 2 );
     325    $source_width = $destWidth ;
     326  }
     327
     328  return array('x' => $x, 'y' => $y);
     329}
     330
    328331add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5);
    329332add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 7);
  • extensions/square_thumbnails/trunk/thumbnail.php

    r9754 r9770  
    4646
    4747      <li>
    48         <span class="property">{\'Square Thumbnails\'|@translate}</span>
     48        <span class="property"><label for="square">{\'Square Thumbnails\'|@translate}</label></span>
    4949             <label><input type="checkbox" name="square" id="square" {if $params.SQUARE}checked="checked"{/if}></label>
    5050      </li>
Note: See TracChangeset for help on using the changeset viewer.