Jump to:

22993 Posts in 11595 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Aligning a Cropped Image

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

Page: 1 2
Go to End
Author Topic: 2300 Views
  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Aligning a Cropped Image Link to this post

    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;
       }
    }

    2300 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.