Skip to content
Permalink
Browse files

NEW Allow the paddedresize to take another hex value to specify a tra…

…nsparency on the padded color
  • Loading branch information...
nfauchelle authored and dhensby committed Feb 11, 2015
1 parent 2ea07f9 commit dae2295e16caed0c38076fdc5f3ed0a962e8f170
@@ -63,6 +63,19 @@ Image methods are chainable. Example:

:::ss
<body style="background-image:url($Image.ScaleWidth(800).CropHeight(800).Link)">

### Padded Image Resize

The Pad method allows you to resize an image with existing ratio and will
pad any surplus space. You can specify the color of the padding using a hex code such as FFFFFF or 000000.

You can also specify a level of transparency to apply to the padding color in a fourth param. This will only effect
png images.

:::php
$Image.Pad(80, 80, FFFFFF, 50) // white padding with 50% transparency
$Image.Pad(80, 80, FFFFFF, 100) // white padding with 100% transparency
$Image.Pad(80, 80, FFFFFF) // white padding with no transparency

### Manipulating images in PHP

@@ -417,14 +417,20 @@ public function resizeRatio( $maxWidth, $maxHeight, $useAsMinimum = false ) {
return $useAsMinimum ? $this->resizeByWidth( $maxWidth ) : $this->resizeByHeight( $maxHeight );
}
public static function color_web2gd($image, $webColor) {
public static function color_web2gd($image, $webColor, $transparencyPercent = 0) {
if(substr($webColor,0,1) == "#") $webColor = substr($webColor,1);
$r = hexdec(substr($webColor,0,2));
$g = hexdec(substr($webColor,2,2));
$b = hexdec(substr($webColor,4,2));
if($transparencyPercent) {
if($transparencyPercent > 100) {
$transparencyPercent = 100;
}
$a = 127 * bcdiv($transparencyPercent, 100, 2);
return imagecolorallocatealpha($image, $r, $g, $b, $a);
}
return imagecolorallocate($image, $r, $g, $b);
}
/**
@@ -433,8 +439,11 @@ public static function color_web2gd($image, $webColor) {
* @param width
* @param height
* @param backgroundColour
* @param transparencyPercent
*/
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
public function paddedResize($width, $height, $backgroundColor = "FFFFFF", $transparencyPercent = 0) {
//keep the % within bounds of 0-100
$transparencyPercent = min(100, max(0, $transparencyPercent));
if(!$this->gd) return;
$width = round($width);
$height = round($height);
@@ -450,7 +459,7 @@ public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
imagealphablending($newGD, false);
imagesavealpha($newGD, true);
$bg = GD::color_web2gd($newGD, $backgroundColor);
$bg = GD::color_web2gd($newGD, $backgroundColor, $transparencyPercent);
imagefilledrectangle($newGD, 0, 0, $width, $height, $bg);
$destAR = $width / $height;
@@ -194,18 +194,45 @@ public function resizeByHeight($height) {
*
* @param int $width
* @param int $height
* @param int $transparencyPercent
* @return Image_Backend
*/
public function paddedResize($width, $height, $backgroundColor = "FFFFFF") {
public function paddedResize($width, $height, $backgroundColor = "FFFFFF", $transparencyPercent = 0) {
//keep the % within bounds of 0-100
$transparencyPercent = min(100, max(0, $transparencyPercent));
$new = $this->resizeRatio($width, $height);
$new->setImageBackgroundColor("#".$backgroundColor);
if($transparencyPercent) {
$alphaHex = $this->calculateAlphaHex($transparencyPercent);
$new->setImageBackgroundColor("#{$backgroundColor}{$alphaHex}");
} else {
$new->setImageBackgroundColor("#{$backgroundColor}");
}
$w = $new->getImageWidth();
$h = $new->getImageHeight();
$new->extentImage($width,$height,($w-$width)/2,($h-$height)/2);
return $new;
}
/**
* Convert a percentage (or 'true') to a two char hex code to signifiy the level of an alpha channel
*
* @param $percent
* @return string
*/
public function calculateAlphaHex($percent) {
if($percent > 100) {
$percent = 100;
}
// unlike GD, this uses 255 instead of 127, and is reversed. Lower = more transparent
$alphaHex = dechex(255 - floor(255 * bcdiv($percent, 100, 2)));
if(strlen($alphaHex) == 1) {
$alphaHex = '0' .$alphaHex;
}
return $alphaHex;
}
/**
* croppedResize
*
@@ -339,12 +339,13 @@ public function FillMax($width, $height) {
*
* @param integer $width The width to size to
* @param integer $height The height to size to
* @param integer $transparencyPercent Level of transparency
* @return Image|null
*/
public function Pad($width, $height, $backgroundColor='FFFFFF') {
public function Pad($width, $height, $backgroundColor='FFFFFF', $transparencyPercent = 0) {
return $this->isSize($width, $height) && !Config::inst()->get('Image', 'force_resample')
? $this
: $this->getFormattedImage('Pad', $width, $height, $backgroundColor);
: $this->getFormattedImage('Pad', $width, $height, $backgroundColor, $transparencyPercent);
}
/**
@@ -654,12 +655,13 @@ public function generateStripThumbnail(Image_Backend $backend) {
*
* @param integer $width The width to size to
* @param integer $height The height to size to
* @param integer $transparencyPercent Level of transparency
* @return Image
* @deprecated 4.0 Use Pad instead
*/
public function PaddedImage($width, $height, $backgroundColor='FFFFFF') {
public function PaddedImage($width, $height, $backgroundColor='FFFFFF', $transparencyPercent = 0) {
Deprecation::notice('4.0', 'Use Pad instead');
return $this->Pad($width, $height, $backgroundColor);
return $this->Pad($width, $height, $backgroundColor, $transparencyPercent);
}
/**
@@ -668,12 +670,13 @@ public function PaddedImage($width, $height, $backgroundColor='FFFFFF') {
* @param Image_Backend $backend
* @param integer $width The width to size to
* @param integer $height The height to size to
* @param integer $transparencyPercent Level of transparency
* @return Image_Backend
* @deprecated 4.0 Generate methods are no longer applicable
*/
public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') {
public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor = 'FFFFFF', $transparencyPercent = 0) {
Deprecation::notice('4.0', 'Generate methods are no longer applicable');
return $backend->paddedResize($width, $height, $backgroundColor);
return $backend->paddedResize($width, $height, $backgroundColor, $transparencyPercent);
}
/**
@@ -1045,7 +1048,7 @@ public function onAfterUpload() {
protected function onBeforeDelete() {
$backend = Injector::inst()->createWithArgs(self::config()->backend, array(
Director::baseFolder()."/" . $this->Filename
Director::baseFolder()."/" . $this->Filename
));
$backend->onBeforeDelete($this);
@@ -282,10 +282,10 @@ public function testGenerateImageWithInvalidParameters() {
public function testCacheFilename() {
$image = $this->objFromFixture('Image', 'imageWithoutTitle');
$imageFirst = $image->Pad(200,200,'CCCCCC');
$imageFirst = $image->Pad(200,200,'CCCCCC', 0);
$imageFilename = $imageFirst->getFullPath();
// Encoding of the arguments is duplicated from cacheFilename
$neededPart = 'Pad' . Convert::base64url_encode(array(200,200,'CCCCCC'));
$neededPart = 'Pad' . Convert::base64url_encode(array(200,200,'CCCCCC', 0));
$this->assertContains($neededPart, $imageFilename, 'Filename for cached image is correctly generated');
}
@@ -308,7 +308,12 @@ public function testMultipleGenerateManipulationCalls_Regeneration() {
$imageThird = $imageSecond->Pad(600,600,'0F0F0F');
// Encoding of the arguments is duplicated from cacheFilename
$argumentString = Convert::base64url_encode(array(600,600,'0F0F0F'));
$argumentString = Convert::base64url_encode(array(
600,
600,
'0F0F0F',
0
));
$this->assertNotNull($imageThird);
$this->assertContains($argumentString, $imageThird->getFullPath(),
'Image contains background color for padded resizement');

0 comments on commit dae2295

Please sign in to comment.
You can’t perform that action at this time.