Example #1
0
 /**
  * Create a cropped version of a picture.
  *
  * Note that cropped images are *always* created as jpeg image files!
  * @param   string  $source_path    The source image file path
  * @param   string  $target_path    The target image file path
  * @param   integer $x1             The left border of the cropped image
  * @param   integer $y1             The top border of the cropped image
  * @param   integer $x2             The right border of the cropped image
  * @param   integer $y2             The bottom border of the cropped image
  * @param   boolean $force          If true, the target image is forced
  *                                  to be overwritten
  * @param   integer $quality        The desired jpeg thumbnail quality
  * @return  bool                    True on success, false otherwise.
  * @static
  */
 static function crop($source_path, $target_path, $x1, $y1, $x2, $y2, $force = false, $quality = 90)
 {
     //DBG::log("crop($source_path, $target_path, $x1, $y1, $x2, $y2, $force, $quality): Entered");
     File::path_relative_to_root($source_path);
     $xs = $ys = null;
     list($xs, $ys) = getimagesize(ASCMS_DOCUMENT_ROOT . '/' . $source_path);
     // Fix coordinates that are out of range:
     // - Reset negative and too large values to the original size
     if ($x1 < 0) {
         $x1 = 0;
     }
     if ($y1 < 0) {
         $y1 = 0;
     }
     if ($x2 < 0) {
         $x2 = $xs - 1;
     }
     if ($y2 < 0) {
         $y2 = $ys - 1;
     }
     if ($x1 >= $xs) {
         $x1 = 0;
     }
     if ($y1 >= $ys) {
         $y1 = 0;
     }
     if ($x2 >= $xs) {
         $x2 = $xs - 1;
     }
     if ($y2 >= $ys) {
         $y2 = $ys - 1;
     }
     // - Flip left and right or top and bottom if the former are greater
     if ($x1 > $x2) {
         $tmp = $x1;
         $x1 = $x2;
         $x2 = $tmp;
     }
     if ($y1 > $y2) {
         $tmp = $y1;
         $y1 = $y2;
         $y2 = $tmp;
     }
     // Target size is now at most the original size.
     // Calculate target size
     $xs = $x2 - $x1;
     $ys = $y2 - $y1;
     $source_image = self::load($source_path);
     if (!$source_image) {
         return false;
     }
     $target_image = false;
     if (function_exists('imagecreatetruecolor')) {
         $target_image = @imagecreatetruecolor($xs, $ys);
     }
     if (!$target_image) {
         $target_image = ImageCreate($xs, $ys);
     }
     imagecopy($target_image, $source_image, 0, 0, $x1, $y1, $xs, $ys);
     return self::saveJpeg($target_image, $target_path, $quality, $force);
 }