Ejemplo n.º 1
0
 /**
  *  Resize Image
  *
  *  @param  string   $sourceFile           Source image file
  *  @param  string   $targetFile           Target image file
  *  @param  integer  $width                Image width
  *  @param  integer  $height               Image height
  *  @param  boolean  $resizeIfSmaller      Resize if image size smaller
  *  @param  boolean  $maintainAspectRatio  Maintain aspect ration of image
  *  @param  boolean  $fixedResize          Fixed resize (crop if needed)
  *
  *  return  boolean  True if resized, False if not resized
  *
  */
 public function ResizeImage($sourceFile, $targetFile, $width, $height, $resizeIfSmaller = false, $maintainAspectRatio = true, $fixedResize = false)
 {
     global $rootPath;
     require_once $rootPath . INC_IMAGETRANSFORM . "class.imagetransform.php";
     $imgTransformer = new imageTransform();
     $imgTransformer->jpegOutputQuality = 90;
     $imgTransformer->sourceFile = $sourceFile;
     $imgTransformer->targetFile = $targetFile;
     $imgTransformer->resizeIfSmaller = $resizeIfSmaller;
     $imgTransformer->maintainAspectRatio = $maintainAspectRatio;
     if ($fixedResize && $maintainAspectRatio) {
         $imgTransformer->readSourceImageSize();
         $relativePercent = $height * 100 / $width;
         $correctHeight = floor($imgTransformer->sourceImageWidth / 100 * $relativePercent);
         if (abs($imgTransformer->sourceImageHeight - $correctHeight) > 1 && $imgTransformer->sourceImageHeight > $correctHeight) {
             $imgTransformer->resizeToWidth = $width;
             $imgTransformer->resize();
             $imgTransformer->readTargetImageSize();
             $imgTransformer->sourceFile = $targetFile;
             $imgTransformer->crop(0, round(($imgTransformer->targetImageHeight - $height) / 2), $width, $height + round(($imgTransformer->targetImageHeight - $height) / 2));
         } elseif (abs($imgTransformer->sourceImageHeight - $correctHeight) > 1 && $imgTransformer->sourceImageHeight < $correctHeight) {
             $imgTransformer->resizeToHeight = $height;
             $imgTransformer->resize();
             $imgTransformer->readTargetImageSize();
             $imgTransformer->sourceFile = $targetFile;
             $imgTransformer->crop(round(($imgTransformer->targetImageWidth - $width) / 2), 0, $width + round(($imgTransformer->targetImageWidth - $width) / 2), $height);
         } else {
             $imgTransformer->resizeToWidth = $width;
             $imgTransformer->resizeToHeight = $height;
             $imgTransformer->resize();
         }
     } else {
         $imgTransformer->resizeToWidth = $width;
         $imgTransformer->resizeToHeight = $height;
         $imgTransformer->resize();
     }
     if ($imgTransformer->error != "") {
         $this->SetError($imgTransformer->error, __FILE__, __LINE__);
         return false;
     } else {
         return true;
     }
 }
Ejemplo n.º 2
0
            $image->targetFile = DIRECTORY . $temp_image;
            if ($image->rotate(-$_POST['rotate_degrees'])) {
                // Rotate in negative degrees so it goes clockwise
                print json_encode(array('success' => true, 'message' => 'Image successfully rotated'));
            } else {
                print json_encode(array('success' => false, 'message' => 'Error: Could not rotate image'));
            }
        } else {
            print json_encode(array('success' => false, 'message' => 'Error: No image specified'));
        }
        exit;
        break;
    case "crop_image":
        if ($_POST['image']) {
            // Make sure we are editing our temp image, and not the original
            $temp_image = str_replace(basename($_POST['image']), '_fm_' . basename($_POST['image']), $_POST['image']);
            $image = new imageTransform();
            $image->jpegOutputQuality = 80;
            $image->sourceFile = DIRECTORY . $temp_image;
            $image->targetFile = DIRECTORY . $temp_image;
            if ($image->crop($_POST['crop_x'], $_POST['crop_y'], $_POST['crop_x'] + $_POST['crop_width'], $_POST['crop_y'] + $_POST['crop_height'])) {
                print json_encode(array('success' => true, 'message' => 'Image successfully cropped'));
            } else {
                print json_encode(array('success' => false, 'message' => 'Error: Could not crop image'));
            }
        } else {
            print json_encode(array('success' => false, 'message' => 'Error: No image specified'));
        }
        exit;
        break;
}