option crop mw_thumbs.0.4
authordj3c1t <dj3c1t@free.fr>
Thu, 15 May 2014 21:53:18 +0000 (23:53 +0200)
committerdj3c1t <dj3c1t@free.fr>
Thu, 15 May 2014 21:53:18 +0000 (23:53 +0200)
app/data/modules/share/mw_data_thumbs.php

index 15f0799..c7cf3a4 100644 (file)
@@ -6,7 +6,7 @@
       return true;
     }
 
-    function img_thumb($src, $max_width, $max_height, $thumbs_dir = "", $background_color = array(255, 255, 255)){
+    function img_thumb($src, $max_width, $max_height, $thumbs_dir = "", $background_color = array(255, 255, 255), $CROP = false){
       if(strlen($thumbs_dir) > 0){
         if(!@is_dir($thumbs_dir)) @mkdir($thumbs_dir);
         if(!@is_dir($thumbs_dir)) return false;
@@ -61,7 +61,8 @@
             $thumbs_dir,
             $thumb_file,
             null,
-            $background_color
+            $background_color,
+            $CROP
           )
         ) === false
       ){
@@ -97,7 +98,8 @@
       $thumbs_dir,
       $thumb_file,
       $quality = null,
-      $background_color = array(255, 255, 255)
+      $background_color = array(255, 255, 255),
+      $CROP = false
     ){
       $dest = $thumbs_dir.$thumb_file;
       if(!($size = @getimagesize($src))) return false;
           $thumb_img = imagecreatetruecolor($max_width, $max_height);
           $thumb_bkg = imagecolorallocate($thumb_img, $background_color[0], $background_color[1], $background_color[2]);
           imagefilledrectangle($thumb_img, 0, 0, $max_width, $max_height, $thumb_bkg);
+          if($CROP){
+            if($size[0] < $max_width || $size[1] < $max_height){
+              $CROP = false;
+            }
+            if($width == $max_width && $height == $max_height){
+              $CROP = false;
+            }
+          }
+          if($CROP){
+            $dst_x = 0;
+            $dst_y = 0;
+            $dst_w = $max_width;
+            $dst_h = $max_height;
+            if($width != $max_width){
+              $r = $max_width / $size[0];
+              $thumb_cut_height = (($size[1] * $r) - $max_height) / 2;
+              $img_cut_height = $size[1] * ($thumb_cut_height / $max_height);
+              $src_x = 0;
+              $src_y = floor($img_cut_height);
+              $src_w = $size[0];
+              $src_h = floor($size[1] - (2 * $img_cut_height));
+            }
+            else{
+              $r = $max_height / $size[1];
+              $thumb_cut_width = (($size[0] * $r) - $max_width) / 2;
+              $img_cut_width = $size[0] * ($thumb_cut_width / $max_width);
+              $src_x = floor($img_cut_width);
+              $src_y = 0;
+              $src_w = floor($size[0] - (2 * $img_cut_width));
+              $src_h = $size[1];
+            }
+          }
+          else{
+            $dst_x = floor(($max_width - $width) / 2);
+            $dst_y = floor(($max_height - $height) / 2);
+            $src_x = 0;
+            $src_y = 0;
+            $dst_w = $width;
+            $dst_h = $height;
+            $src_w = $size[0];
+            $src_h = $size[1];
+          }
           imagecopyresampled(
             $thumb_img,
             $src_img,
-            floor(($max_width - $width) / 2),
-            floor(($max_height - $height) / 2),
-            0,
-            0,
-            $width,
-            $height,
-            $size[0],
-            $size[1]
+            $dst_x,
+            $dst_y,
+            $src_x,
+            $src_y,
+            $dst_w,
+            $dst_h,
+            $src_w,
+            $src_h
           );
           if(isset($quality)) $thumb_img = $thumb_function($thumb_img, $dest, $quality);
           else $thumb_img = $thumb_function($thumb_img, $dest);