Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Aligning a Cropped Image


Go to End
Reply


9 Posts   2735 Views

Avatar
pinkp

Community Member, 168 Posts

6 June 2013 at 10:31pm

Edited: 06/06/2013 10:50pm

I also found this code which works perfectly for me and allows the user to choose the crop from area in the CMS:
http://sspaste.com/paste/show/5192998f4f141
Just call:
$Image.CroppedImagePriority(200,150)
rather than
$Image1.CroppedImage(200,150)

//config.php

Object::add_extension('File', 'CroppedImagePlus');

// Dataobject getCMSFields

$OverviewMapPicField = new UploadField('OverviewMapPic', 'Overview Map Thumbnail (520x500) or (520x317), harbourpage (292x114)');
      $OverviewMapPicField->allowedExtensions = array('jpg', 'png', 'gif');
      $OverviewMapPicField->folderName = 'Uploads/'.$parenturl.'/Maps';
      $OverviewMapPicField->setConfig('fileEditFields', 'priorityField');
      $fields->addFieldToTab('Root.Maps', $OverviewMapPicField);


// CroppedImagePlus.php

<?php

class CroppedImagePlus extends DataExtension
{
   static $db = array(
      "CropPriority" => "Enum('Center, Top, Left, Bottom, Right', 'Center')"
   );

public function CroppedImagePriority($width, $height)
{

   $dimensions = $width."_".$height;
      $croppriority = $this->owner->CropPriority;

      // If priority default make default ss crop or any dimension is too small
      if ($croppriority == 'Center' || $this->owner->getWidth() < $width || $this->owner->getHeight() < $height) {
         $output = $this->owner->getFormattedImage('CroppedImage', $width, $height);
      }
      // Lets make a perfect thumb
      else {
         $output = $this->owner->getFormattedImage('CroppedImagePriority', $dimensions, $croppriority);
      }

return $output;
}

public function generateCroppedImagePriority(GD $gd, $dimensions, $croppriority)
{

   $widhtheight = explode('_', $dimensions);
   $width = $widhtheight[0];
   $height = $widhtheight[1];
   $image = $this->owner;

   $origWidth = $image->getWidth();
      $origHeight = $image->getHeight();

      // If size matches do nothing
      if ($width == $origWidth && $height == $origHeight) {
         return $gd;
      }

      $destAR = $width / $height; // Külgede suhe
      $srcAR = $origWidth / $origHeight; // Uue pildi külgede suhe

      // Destination narrower than the source
      if($destAR < $srcAR) {

         // Teeme kõrguse parajaks
         $gd = $gd->resizeByHeight($height);

         $overwidth = round($gd->getWidth() - $width);

         if ($image->CropPriority == 'Left') {
            $gd = $gd->crop(0, 0, $width, $height);
         }
         elseif ($image->CropPriority == 'Right') {
            $gd = $gd->crop(0, $overwidth, $width, $height);
         }
         else { // Kui prioriteeti pole antud suunal
            $gd = $gd->crop(0, $overwidth/2, $width, $height);
         }

      // Destination shorter than the source
      } else {

         $gd = $gd->resizeByWidth($width);   // Vähendame küljed sobivaks

         $overheight = round($gd->getHeight() - $height);   // Palju maha lõikame

         if ($image->CropPriority == 'Top') {
            $gd = $gd->crop(0, 0, $width, $height);
         }
         elseif ($image->CropPriority == 'Bottom') {
            $gd = $gd->crop($overheight, 0, $width, $height);
         }
         else { // Kui prioriteeti pole antud suunal
            $gd = $gd->crop($overheight/2, 0, $width, $height);
         }
      }

      return $gd;
}

   public function priorityField() {
      $fields = $this->owner->getCMSFields();

      //Crop Priority Field for files.
      $CropPriorityField = new DropdownField('CropPriority', 'Crop Priority', $this->owner->dbObject('CropPriority')->enumValues());
      $fields->addFieldToTab('Root.Main', $CropPriorityField);

      // Stole it from UploadField.php
      // Only display main tab, to avoid overly complex interface
      if($fields->hasTabSet() && $mainTab = $fields->findOrMakeTab('Root.Main')) $fields = $mainTab->Fields();
      return $fields;
   }
}

Go to Top