1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-23 05:52:47 +02:00

Merged in the psr2 compliant code

This commit is contained in:
Eric Jones
2013-09-20 15:28:42 -04:00
2 changed files with 65 additions and 56 deletions

View File

@@ -167,10 +167,7 @@ class Image
public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false)
{
if (!class_exists('\Intervention\Image\ImageCache')) {
throw new Exception\ImageCacheNotFoundException(
'Please install package intervention/imagecache before
running this function.'
);
throw new Exception('Please install package intervention/imagecache before running this function.');
}
// Create image and run callback
@@ -189,9 +186,7 @@ class Image
private function initFromPath($path)
{
if (!file_exists($path)) {
throw new Exception\ImageNotFoundException(
"Image file ({$path}) not found"
);
throw new Exception("Image file ({$path}) not found");
}
// set file info
@@ -220,7 +215,7 @@ class Image
private function initFromResource($resource)
{
if (!$this->isImageResource($resource)) {
throw new Exception\InvalidImageResourceException;
throw new Exception("initFromResource expects parameter to be resource.");
}
$this->setImageInfoFromResource($resource);
@@ -352,8 +347,8 @@ class Image
$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;
}
@@ -632,7 +627,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;
@@ -1283,7 +1278,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':
@@ -1345,21 +1346,27 @@ class Image
} 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;
@@ -1523,7 +1530,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;
}
}
}
@@ -1583,9 +1592,7 @@ class Image
break;
default:
throw new Exception\InvalidImageTypeException(
"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;
}
}

View File

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