Exemple #1
0
 /**
  * Scales an image down to smaller than the max_width and max_height.
  * You cannot scale an image to a higher resolution.
  */
 public static function scaleImage($source_dir, $dest_dir, $max_width, $max_height)
 {
     if (empty($max_width) || empty($max_height)) {
         return false;
     }
     if (!extension_loaded('gd')) {
         if (!dl('gd.so')) {
             copy(PHPWS_SOURCE_DIR . 'core/img/nogd.png', $dest_dir);
             return true;
         }
     }
     $size = getimagesize($source_dir);
     if (empty($size)) {
         return false;
     }
     $width =& $size[0];
     $height =& $size[1];
     $file_type =& $size['mime'];
     // if image is already smaller than the limits, just copy it
     if ($width <= $max_width && $height <= $max_height) {
         if ($source_dir != $dest_dir) {
             return copy($source_dir, $dest_dir);
         } else {
             return true;
         }
     }
     $diff = PHPWS_File::getDiff($width, $max_width, $height, $max_height);
     $new_width = round($width * $diff);
     $new_height = round($height * $diff);
     if ($new_width > $max_width || $new_height > $max_height) {
         $diff = PHPWS_File::getDiff($new_width, $max_width, $new_height, $max_height);
         $new_width = round($width * $diff);
         $new_height = round($height * $diff);
     }
     if ($new_width > $max_width || $new_height > $max_height) {
         $new_width = round($new_width * $diff);
         $new_height = round($new_height * $diff);
     }
     $source_image = PHPWS_File::_imageCopy($source_dir, $file_type);
     $resampled_image = PHPWS_File::_resampleImage($new_width, $new_height);
     imagecopyresampled($resampled_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     imagedestroy($source_image);
     $result = PHPWS_File::_writeImageCopy($resampled_image, $dest_dir, $file_type);
     if (!$result) {
         imagedestroy($resampled_image);
         return false;
     }
     chmod($dest_dir, 0644);
     imagedestroy($resampled_image);
     return true;
 }