14/05/2013 10:07pm

PHP | Working Example - Cropped image pluss


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


Post Comment