To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Show
Ignore:
Timestamp:
08/08/10 12:18:34 (22 months ago)
Author:
tboutell
Message:

Merged the cropping branch back to trunk:

Cropping is now available in the media repository. Every time you select an image for use on the site you have the opportunity to crop it. The image size constraints of the slot are taken into account. This means that you can satisfy a given set of constraints by cropping a wide variety of images that would not have been eligible for selection before.

Crops of existing images appear in the database as separate aMediaItem objects but do not duplicate the original image file. This means that many crops of a single source image can exist without significant overhead.

The slug field of a cropped version of an image contains cropping parameters. When rendering the image, Symfony routes spot these and crop the original file as needed.

By approaching the problem this way we have avoided the need for code that takes advantage of the media repository's image selection capabilities to change in any way in order to take advantage of cropping.

The single-image-select behavior of Apostrophe has changed to accommodate the extra cropping step. Single select for PDFs and videos still uses the selectSingle partial because they cannot be cropped.

Thanks to Spike Broehm for his contributions to the cropping project.

Fixes #227

Location:
plugins/apostrophePlugin/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • plugins/apostrophePlugin/trunk

  • plugins/apostrophePlugin/trunk/lib/toolkit/aImageConverter.class.php

    r1872 r1917  
    5555  } 
    5656 
    57   static public function cropOriginal($fileIn, $fileOut, $width, $height, $quality = 75) 
    58   { 
     57  // $width and $height are the dimensions of the final rendered image. $quality is the JPEG quality setting (where needed). 
     58  // The $crop parameters, when not null (all four must be null or not null), are used to crop the original before scaling/distorting  
     59  // to the specified width and height and are always in the original image's coordinates. 
     60   
     61  // If cropping coordinates are not specified, the largest possible portion of the center of the original image is scaled to fit into the  
     62  // destination image without distortion 
     63   
     64  static public function cropOriginal($fileIn, $fileOut, $width, $height, $quality = 75, $cropLeft = null, $cropTop = null, $cropWidth = null,  $cropHeight = null) 
     65  { 
     66    // Allow skipping of parameters 
     67    if (is_null($quality)) 
     68    { 
     69      $quality = 75; 
     70    } 
    5971    $width = ceil($width); 
    6072    $height = ceil($height); 
     
    6779    $iratio = $iwidth / $iheight; 
    6880    $ratio = $width / $height; 
     81 
     82     // Spike's contribution: arbitrary cropping 
     83     if (!is_null($cropWidth) && !is_null($cropHeight) && !is_null($cropLeft) && !is_null($cropTop)) 
     84     { 
     85       $cropTop = ceil($cropTop + 0); 
     86       $cropLeft = ceil($cropLeft + 0); 
     87       $cropWidth = ceil($cropWidth + 0); 
     88       $cropHeight = ceil($cropHeight + 0); 
     89        
     90       $scale = array('xysize' => array($width + 0, $height + 0)); 
     91       $crop = array('left' => $cropLeft, 'top' => $cropTop, 'width' => $cropWidth, 'height' => $cropHeight); 
     92       return self::scaleBody($fileIn, $fileOut, $scale, $crop, $quality); 
     93     } 
    6994 
    7095    $scale = array('xysize' => array($width + 0, $height + 0)); 
     
    356381        $width = $scaleParameters['xysize'][0]; 
    357382        $height = $scaleParameters['xysize'][1]; 
    358          
     383        // This was backwards until 05/31/2010, making things bigger rather than smaller if their 
     384        // aspect ratios differed from the original. Be consistent with netpbm which makes things 
     385        // smaller not bigger 
    359386        if (($width / $height) > ($swidth / $sheight)) 
    360387        { 
    361           // Wider than the original. So it will be shorter than requested 
    362           $height = ceil($width * ($sheight / $swidth)); 
     388          // Wider than the original. So it will be narrower than requested 
     389          $width = ceil($height * ($swidth / $sheight)); 
    363390        } 
    364391        else 
    365392        { 
    366           // Taller than the original. So it will be narrower than requested 
    367           $width = ceil($height * ($swidth / $sheight)); 
     393          // Taller than the original. So it will be shorter than requested 
     394          $height = ceil($width * ($sheight / $swidth)); 
    368395        } 
    369396        $out = self::createTrueColorAlpha($width, $height);