1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-13 17:34:04 +02:00

Fix the code to be PSR-2 compliant

This commit is contained in:
Eric Jones
2013-09-20 15:15:46 -04:00
parent 62ec665340
commit 5c459429a0
2 changed files with 62 additions and 46 deletions

View File

@@ -95,7 +95,7 @@ class Image
public function __construct($source = null, $width = null, $height = null, $bgcolor = null) public function __construct($source = null, $width = null, $height = null, $bgcolor = null)
{ {
// set image properties // set image properties
if ( ! is_null($source)) { if (!is_null($source)) {
if ($this->isImageResource($source)) { if ($this->isImageResource($source)) {
@@ -166,7 +166,7 @@ class Image
*/ */
public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false) 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.'); throw new Exception('Please install package intervention/imagecache before running this function.');
} }
@@ -185,7 +185,7 @@ class Image
*/ */
private function initFromPath($path) private function initFromPath($path)
{ {
if ( ! file_exists($path)) { if (!file_exists($path)) {
throw new Exception("Image file ({$path}) not found"); throw new Exception("Image file ({$path}) not found");
} }
@@ -214,7 +214,7 @@ class Image
*/ */
private function initFromResource($resource) private function initFromResource($resource)
{ {
if ( ! $this->isImageResource($resource)) { if (!$this->isImageResource($resource)) {
throw new Exception("initFromResource expects parameter to be resource."); throw new Exception("initFromResource expects parameter to be resource.");
} }
@@ -265,7 +265,7 @@ class Image
* @param integer $src_h * @param integer $src_h
* @return Image * @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 // create new image
$image = imagecreatetruecolor($dst_w, $dst_h); $image = imagecreatetruecolor($dst_w, $dst_h);
@@ -275,7 +275,7 @@ class Image
imagesavealpha($image, true); imagesavealpha($image, true);
// copy content from resource // 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 // set new content as recource
$this->resource = $image; $this->resource = $image;
@@ -334,7 +334,7 @@ class Image
// If both width and hight have been passed along, the width and // If both width and hight have been passed along, the width and
// height parameters are maximum values. // height parameters are maximum values.
if ( ! is_null($width) && ! is_null($height)) { if (! is_null($width) && ! is_null($height)) {
// First, calculate the height. // First, calculate the height.
$height = intval($width / $this->width * $this->height); $height = intval($width / $this->width * $this->height);
@@ -346,9 +346,9 @@ class Image
$height = $max_height; $height = $max_height;
$width = intval($height / $this->height * $this->width); $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; $width = is_null($width) ? intval($height / $this->height * $this->width) : $width;
$height = is_null($height) ? intval($width / $this->width * $this->height) : $height; $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 // If the image can't be upsized, check if the given width and/or
// height are too large. // height are too large.
if ( ! $upsize) { if (! $upsize) {
// If the given width is larger then the image width, // If the given width is larger then the image width,
// then don't resize it. // then don't resize it.
if ( ! is_null($width) && $width > $this->width) { if (! is_null($width) && $width > $this->width) {
$width = $this->width; $width = $this->width;
// If ratio needs to be kept, height is recalculated. // 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, // If the given height is larger then the image height,
// then don't resize it. // then don't resize it.
if ( ! is_null($height) && $height > $this->height) { if (! is_null($height) && $height > $this->height) {
$height = $this->height; $height = $this->height;
// If ratio needs to be kept, width is recalculated. // 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); $dst_y = ($height <= $this->height) ? 0 : intval(($height - $this->height) / 2);
// copy content from resource // 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 // set new content as recource
$this->resource = $image; $this->resource = $image;
@@ -603,7 +603,7 @@ class Image
throw new Exception('width and height of cutout needs to be defined'); 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; $width = is_numeric($width) ? intval($width) : null;
$height = is_numeric($height) ? intval($height) : 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 // if width or height are not set, define values automatically
$width = is_null($width) ? $height : $width; $width = is_null($width) ? $height : $width;
$height = is_null($height) ? $width : $height; $height = is_null($height) ? $width : $height;
@@ -638,7 +638,7 @@ class Image
$grab_width = $this->width; $grab_width = $this->width;
$ratio = $grab_width / $width; $ratio = $grab_width / $width;
if($height * $ratio <= $this->height) { if ($height * $ratio <= $this->height) {
$grab_height = round($height * $ratio); $grab_height = round($height * $ratio);
$src_x = 0; $src_x = 0;
$src_y = round(($this->height - $grab_height) / 2); $src_y = round(($this->height - $grab_height) / 2);
@@ -1139,7 +1139,7 @@ class Image
*/ */
public function blur($amount = 1) 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); imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR);
} }
@@ -1225,7 +1225,7 @@ class Image
case 'gif': case 'gif':
case 1: case 1:
imagegif($this->resource); imagegif($this->resource);
break; break;
case 'png': case 'png':
case 3: case 3:
@@ -1233,14 +1233,14 @@ class Image
imagealphablending($this->resource, false); imagealphablending($this->resource, false);
imagesavealpha($this->resource, true); imagesavealpha($this->resource, true);
imagepng($this->resource, null, $quality); imagepng($this->resource, null, $quality);
break; break;
default: default:
case 'jpg': case 'jpg':
case 'jpeg': case 'jpeg':
case 2: case 2:
imagejpeg($this->resource, null, $quality); imagejpeg($this->resource, null, $quality);
break; break;
} }
$data = ob_get_contents(); $data = ob_get_contents();
@@ -1271,7 +1271,13 @@ class Image
case 'rgba': case 'rgba':
$color = imagecolorsforindex($this->resource, $color); $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; break;
case 'hex': case 'hex':
@@ -1314,7 +1320,7 @@ class Image
// color is alread allocated // color is alread allocated
$allocatedColor = $value; $allocatedColor = $value;
} elseif(is_array($value)) { } elseif (is_array($value)) {
// parse color array like: array(155, 155, 155) // parse color array like: array(155, 155, 155)
if (count($value) == 4) { 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 // 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]; $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]; $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]; $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($rgbPattern, $value, $matches)) {
} elseif (preg_match('/^rgb ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\)$/i', $value, $matches)) {
$r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 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($rgbaPattern, $value, $matches)) {
} elseif (preg_match('/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i', $value, $matches)) {
$r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0; $r = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0; $g = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0; $b = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
$a = $this->alpha2gd($matches[4]); $a = $this->alpha2gd($matches[4]);
} }
} }
@@ -1396,7 +1408,7 @@ class Image
{ {
$data = exif_read_data($this->dirname .'/'. $this->basename, 'EXIF', false); $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; 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 ($x=0; $x<$this->width; $x=$x+$step_x) {
for ($y=0; $y<$this->height; $y=$y+$step_y) { for ($y=0; $y<$this->height; $y=$y+$step_y) {
$color = $this->pickColor($x, $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) { switch ($this->type) {
case IMG_PNG: case IMG_PNG:
case 3: case 3:
$this->resource = imagecreatefrompng($path); $this->resource = imagecreatefrompng($path);
break; break;
case IMG_JPG: case IMG_JPG:
case 2: case 2:
$this->resource = imagecreatefromjpeg($path); $this->resource = imagecreatefromjpeg($path);
break; break;
case IMG_GIF: case IMG_GIF:
case 1: case 1:
$this->resource = imagecreatefromgif($path); $this->resource = imagecreatefromgif($path);
break; break;
default: default:
throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images."); throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images.");
break; break;
} }
} }

View File

@@ -31,9 +31,11 @@ class ImageServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
$this->app['image'] = $this->app->share(function($app) { $this->app['image'] = $this->app->share(
return new Image; function ($app) {
}); return new Image;
}
);
} }
/** /**