From 5c459429a02d31eaae286f376693b076ab768ed4 Mon Sep 17 00:00:00 2001 From: Eric Jones Date: Fri, 20 Sep 2013 15:15:46 -0400 Subject: [PATCH 1/2] Fix the code to be PSR-2 compliant --- src/Intervention/Image/Image.php | 100 ++++++++++-------- .../Image/ImageServiceProvider.php | 8 +- 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 8f2ffb12..9783d8e1 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -95,7 +95,7 @@ class Image public function __construct($source = null, $width = null, $height = null, $bgcolor = null) { // set image properties - if ( ! is_null($source)) { + if (!is_null($source)) { if ($this->isImageResource($source)) { @@ -166,7 +166,7 @@ class Image */ public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false) { - if ( ! class_exists('\Intervention\Image\ImageCache')) { + if (!class_exists('\Intervention\Image\ImageCache')) { throw new Exception('Please install package intervention/imagecache before running this function.'); } @@ -185,7 +185,7 @@ class Image */ private function initFromPath($path) { - if ( ! file_exists($path)) { + if (!file_exists($path)) { throw new Exception("Image file ({$path}) not found"); } @@ -214,7 +214,7 @@ class Image */ private function initFromResource($resource) { - if ( ! $this->isImageResource($resource)) { + if (!$this->isImageResource($resource)) { throw new Exception("initFromResource expects parameter to be resource."); } @@ -265,7 +265,7 @@ class Image * @param integer $src_h * @return Image */ - private function modify($dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h) + private function modify($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { // create new image $image = imagecreatetruecolor($dst_w, $dst_h); @@ -275,7 +275,7 @@ class Image imagesavealpha($image, true); // copy content from resource - imagecopyresampled($image, $this->resource, $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h); + imagecopyresampled($image, $this->resource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // set new content as recource $this->resource = $image; @@ -334,7 +334,7 @@ class Image // If both width and hight have been passed along, the width and // height parameters are maximum values. - if ( ! is_null($width) && ! is_null($height)) { + if (! is_null($width) && ! is_null($height)) { // First, calculate the height. $height = intval($width / $this->width * $this->height); @@ -346,9 +346,9 @@ class Image $height = $max_height; $width = intval($height / $this->height * $this->width); } - - } elseif ($ratio && ( ! is_null($width) OR ! is_null($height))) { // If only one of width or height has been provided. - + + } elseif ($ratio && ( ! is_null($width) or ! is_null($height))) { + // If only one of width or height has been provided. $width = is_null($width) ? intval($height / $this->height * $this->width) : $width; $height = is_null($height) ? intval($width / $this->width * $this->height) : $height; } @@ -356,10 +356,10 @@ class Image // If the image can't be upsized, check if the given width and/or // height are too large. - if ( ! $upsize) { + if (! $upsize) { // If the given width is larger then the image width, // then don't resize it. - if ( ! is_null($width) && $width > $this->width) { + if (! is_null($width) && $width > $this->width) { $width = $this->width; // If ratio needs to be kept, height is recalculated. @@ -370,7 +370,7 @@ class Image // If the given height is larger then the image height, // then don't resize it. - if ( ! is_null($height) && $height > $this->height) { + if (! is_null($height) && $height > $this->height) { $height = $this->height; // If ratio needs to be kept, width is recalculated. @@ -564,7 +564,7 @@ class Image $dst_y = ($height <= $this->height) ? 0 : intval(($height - $this->height) / 2); // copy content from resource - imagecopy($image, $this->resource, $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h); + imagecopy($image, $this->resource, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); // set new content as recource $this->resource = $image; @@ -603,7 +603,7 @@ class Image throw new Exception('width and height of cutout needs to be defined'); } - return $this->modify(0, 0, $pos_x , $pos_y, $width, $height, $width, $height); + return $this->modify(0, 0, $pos_x, $pos_y, $width, $height, $width, $height); } /** @@ -625,7 +625,7 @@ class Image $width = is_numeric($width) ? intval($width) : null; $height = is_numeric($height) ? intval($height) : null; - if ( ! is_null($width) OR ! is_null($height)) { + if (! is_null($width) or ! is_null($height)) { // if width or height are not set, define values automatically $width = is_null($width) ? $height : $width; $height = is_null($height) ? $width : $height; @@ -638,7 +638,7 @@ class Image $grab_width = $this->width; $ratio = $grab_width / $width; - if($height * $ratio <= $this->height) { + if ($height * $ratio <= $this->height) { $grab_height = round($height * $ratio); $src_x = 0; $src_y = round(($this->height - $grab_height) / 2); @@ -1139,7 +1139,7 @@ class Image */ public function blur($amount = 1) { - for ($i=0; $i < intval($amount); $i++) { + for ($i=0; $i < intval($amount); $i++) { imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR); } @@ -1225,7 +1225,7 @@ class Image case 'gif': case 1: imagegif($this->resource); - break; + break; case 'png': case 3: @@ -1233,14 +1233,14 @@ class Image imagealphablending($this->resource, false); imagesavealpha($this->resource, true); imagepng($this->resource, null, $quality); - break; + break; default: case 'jpg': case 'jpeg': case 2: imagejpeg($this->resource, null, $quality); - break; + break; } $data = ob_get_contents(); @@ -1271,7 +1271,13 @@ class Image case 'rgba': $color = imagecolorsforindex($this->resource, $color); - $color = sprintf('rgba(%d, %d, %d, %.2f)', $color['red'], $color['green'], $color['blue'], $this->alpha2rgba($color['alpha'])); + $color = sprintf( + 'rgba(%d, %d, %d, %.2f)', + $color['red'], + $color['green'], + $color['blue'], + $this->alpha2rgba($color['alpha']) + ); break; case 'hex': @@ -1314,7 +1320,7 @@ class Image // color is alread allocated $allocatedColor = $value; - } elseif(is_array($value)) { + } elseif (is_array($value)) { // parse color array like: array(155, 155, 155) if (count($value) == 4) { @@ -1330,30 +1336,36 @@ class Image } - } elseif(is_string($value)) { + } elseif (is_string($value)) { // parse color string in hexidecimal format like #cccccc or cccccc or ccc - if (preg_match('/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i', $value, $matches)) { + $hexPattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i'; + + // parse color string in format rgb(140, 140, 140) + $rgbPattern = '/^rgb ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\)$/i'; + + // parse color string in format rgba(255, 0, 0, 0.5) + $rgbaPattern = '/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i'; + + if (preg_match($hexPattern, $value, $matches)) { $r = strlen($matches[1]) == '1' ? '0x'.$matches[1].$matches[1] : '0x'.$matches[1]; $g = strlen($matches[2]) == '1' ? '0x'.$matches[2].$matches[2] : '0x'.$matches[2]; $b = strlen($matches[3]) == '1' ? '0x'.$matches[3].$matches[3] : '0x'.$matches[3]; - - // parse color string in format rgb(140, 140, 140) - } elseif (preg_match('/^rgb ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\)$/i', $value, $matches)) { + + } elseif (preg_match($rgbPattern, $value, $matches)) { $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; - - // parse color string in format rgba(255, 0, 0, 0.5) - } elseif (preg_match('/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i', $value, $matches)) { - + + } elseif (preg_match($rgbaPattern, $value, $matches)) { + $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; $a = $this->alpha2gd($matches[4]); - + } } @@ -1396,7 +1408,7 @@ class Image { $data = exif_read_data($this->dirname .'/'. $this->basename, 'EXIF', false); - if ( ! is_null($key)) { + if (! is_null($key)) { return array_key_exists($key, $data) ? $data[$key] : null; } @@ -1478,7 +1490,9 @@ class Image for ($x=0; $x<$this->width; $x=$x+$step_x) { for ($y=0; $y<$this->height; $y=$y+$step_y) { $color = $this->pickColor($x, $y); - if ($color['a'] < 1) return true; + if ($color['a'] < 1) { + return true; + } } } @@ -1517,22 +1531,22 @@ class Image switch ($this->type) { case IMG_PNG: case 3: - $this->resource = imagecreatefrompng($path); - break; + $this->resource = imagecreatefrompng($path); + break; case IMG_JPG: case 2: - $this->resource = imagecreatefromjpeg($path); - break; + $this->resource = imagecreatefromjpeg($path); + break; case IMG_GIF: case 1: - $this->resource = imagecreatefromgif($path); - break; + $this->resource = imagecreatefromgif($path); + break; default: - throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images."); - break; + throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images."); + break; } } diff --git a/src/Intervention/Image/ImageServiceProvider.php b/src/Intervention/Image/ImageServiceProvider.php index c46cca3a..1be6e2e0 100644 --- a/src/Intervention/Image/ImageServiceProvider.php +++ b/src/Intervention/Image/ImageServiceProvider.php @@ -31,9 +31,11 @@ class ImageServiceProvider extends ServiceProvider */ public function register() { - $this->app['image'] = $this->app->share(function($app) { - return new Image; - }); + $this->app['image'] = $this->app->share( + function ($app) { + return new Image; + } + ); } /** From c3380185409e707f821c3403faf435b0581f2ef9 Mon Sep 17 00:00:00 2001 From: Eric Jones Date: Mon, 23 Sep 2013 16:14:59 -0400 Subject: [PATCH 2/2] checked that all src files are PSR-2 compliant --- src/Intervention/Image/Image.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index ba017ffb..4cfae990 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -1496,7 +1496,6 @@ class Image // There was a logical error in the program that wasn't considered. // Namely, how should the program handle a valid resource handle // that wasn't a valid image resource handle. - // // Previously this method simply returned false if $input wasn't // a valid image resource handle. But in the constructor, the next // conditional passed the file handle to the Image::isBinary method.