mirror of
https://github.com/Intervention/image.git
synced 2025-08-13 17:34:04 +02:00
preparations for upcoming addon
This commit is contained in:
@@ -46,8 +46,10 @@ Add the facade of this package to the `$aliases` array.
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
* Image::__construct - Create new instance of Image class
|
* Image::__construct - Create new instance of Image class
|
||||||
* Image::make - Open a new image resource from image file or create a new empty image
|
* Image::make - Open a new image resource from image file
|
||||||
* Image::canvas - Create a new empty image resource
|
* Image::canvas - Create a new empty image resource
|
||||||
|
* Image::raw - Create a new image resource with image data from string
|
||||||
|
* Image::open - Open a new image resource from image file
|
||||||
* Image::resize - Resize current image based on given width and/or height
|
* Image::resize - Resize current image based on given width and/or height
|
||||||
* Image::crop - Crop the current image
|
* Image::crop - Crop the current image
|
||||||
* Image::grab - Cut out a detail of the image in given ratio and resize to output size
|
* Image::grab - Cut out a detail of the image in given ratio and resize to output size
|
||||||
|
@@ -84,19 +84,32 @@ class Image
|
|||||||
*/
|
*/
|
||||||
public function __construct($path = null, $width = null, $height = null)
|
public function __construct($path = null, $width = null, $height = null)
|
||||||
{
|
{
|
||||||
|
// create filesystem
|
||||||
$this->filesystem = new Filesystem;
|
$this->filesystem = new Filesystem;
|
||||||
$this->setProperties($path, $width, $height);
|
|
||||||
|
// set image properties
|
||||||
|
if ( ! is_null($path)) {
|
||||||
|
|
||||||
|
$this->setPropertiesFromPath($path);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$this->setPropertiesEmpty($width, $height);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a new image resource from image file or create a new empty image
|
* Open a new image resource from image file
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return Image
|
* @return Image
|
||||||
*/
|
*/
|
||||||
public static function make($path, $width = null, $height = null)
|
public static function make($path)
|
||||||
{
|
{
|
||||||
return new Image($path, $width, $height);
|
$image = new Image;
|
||||||
|
$image->setPropertiesFromPath($path);
|
||||||
|
|
||||||
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,67 +121,86 @@ class Image
|
|||||||
*/
|
*/
|
||||||
public static function canvas($width, $height)
|
public static function canvas($width, $height)
|
||||||
{
|
{
|
||||||
return new Image(null, $width, $height);
|
$image = new Image;
|
||||||
|
$image->setPropertiesEmpty($width, $height);
|
||||||
|
|
||||||
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set local properties for image resource
|
* Create a new image resource with image data from string
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $data
|
||||||
|
* @return Image
|
||||||
*/
|
*/
|
||||||
private function setProperties($path, $width = null, $height = null)
|
public static function raw($string)
|
||||||
{
|
{
|
||||||
if ( ! is_null($path)) {
|
$image = new Image;
|
||||||
|
$image->setPropertiesFromString($string);
|
||||||
|
|
||||||
if ( ! $this->filesystem->exists($path)) {
|
return $image;
|
||||||
throw new Exception("Image file ({$path}) not found");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// set file info
|
private function setPropertiesFromPath($path)
|
||||||
$info = pathinfo($path);
|
{
|
||||||
$this->dirname = $info['dirname'];
|
if ( ! $this->filesystem->exists($path)) {
|
||||||
$this->basename = $info['basename'];
|
throw new Exception("Image file ({$path}) not found");
|
||||||
$this->extension = $info['extension'];
|
|
||||||
$this->filename = $info['filename'];
|
|
||||||
|
|
||||||
// set image info
|
|
||||||
list($this->width, $this->height, $this->type) = @getimagesize($path);
|
|
||||||
|
|
||||||
// set resource
|
|
||||||
switch ($this->type) {
|
|
||||||
case IMG_PNG:
|
|
||||||
case 3:
|
|
||||||
$this->resource = @imagecreatefrompng($path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IMG_JPG:
|
|
||||||
$this->resource = @imagecreatefromjpeg($path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case IMG_GIF:
|
|
||||||
$this->resource = @imagecreatefromgif($path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
$this->width = is_numeric($width) ? intval($width) : 1;
|
|
||||||
$this->height = is_numeric($height) ? intval($height) : 1;
|
|
||||||
|
|
||||||
$this->original['width'] = $this->width;
|
|
||||||
$this->original['height'] = $this->height;
|
|
||||||
|
|
||||||
// create empty image
|
|
||||||
$this->resource = @imagecreatetruecolor($this->width, $this->height);
|
|
||||||
|
|
||||||
// fill with transparent background instead of black
|
|
||||||
$transparent = imagecolorallocatealpha($this->resource, 0, 0, 0, 127);
|
|
||||||
imagefill($this->resource, 0, 0, $transparent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set file info
|
||||||
|
$info = pathinfo($path);
|
||||||
|
$this->dirname = $info['dirname'];
|
||||||
|
$this->basename = $info['basename'];
|
||||||
|
$this->extension = $info['extension'];
|
||||||
|
$this->filename = $info['filename'];
|
||||||
|
|
||||||
|
// set image info
|
||||||
|
list($this->width, $this->height, $this->type) = @getimagesize($path);
|
||||||
|
|
||||||
|
// set resource
|
||||||
|
switch ($this->type) {
|
||||||
|
case IMG_PNG:
|
||||||
|
case 3:
|
||||||
|
$this->resource = @imagecreatefrompng($path);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IMG_JPG:
|
||||||
|
$this->resource = @imagecreatefromjpeg($path);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IMG_GIF:
|
||||||
|
$this->resource = @imagecreatefromgif($path);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Exception("Wrong image type ({$this->type}) only use JPG, PNG or GIF images.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setPropertiesFromString($string)
|
||||||
|
{
|
||||||
|
$this->resource = imagecreatefromstring($string);
|
||||||
|
$this->width = imagesx($this->resource);
|
||||||
|
$this->height = imagesy($this->resource);
|
||||||
|
$this->original['width'] = $this->width;
|
||||||
|
$this->original['height'] = $this->height;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setPropertiesEmpty($width, $height)
|
||||||
|
{
|
||||||
|
$this->width = is_numeric($width) ? intval($width) : 1;
|
||||||
|
$this->height = is_numeric($height) ? intval($height) : 1;
|
||||||
|
|
||||||
|
$this->original['width'] = $this->width;
|
||||||
|
$this->original['height'] = $this->height;
|
||||||
|
|
||||||
|
// create empty image
|
||||||
|
$this->resource = @imagecreatetruecolor($this->width, $this->height);
|
||||||
|
|
||||||
|
// fill with transparent background instead of black
|
||||||
|
$transparent = imagecolorallocatealpha($this->resource, 0, 0, 0, 127);
|
||||||
|
imagefill($this->resource, 0, 0, $transparent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -202,6 +234,19 @@ class Image
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a new image resource from image file
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @return Image
|
||||||
|
*/
|
||||||
|
public function open($path)
|
||||||
|
{
|
||||||
|
$this->setPropertiesFromPath($path);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize current image based on given width/height
|
* Resize current image based on given width/height
|
||||||
*
|
*
|
||||||
@@ -698,11 +743,11 @@ class Image
|
|||||||
{
|
{
|
||||||
if (is_null($this->dirname) && is_null($this->basename)) {
|
if (is_null($this->dirname) && is_null($this->basename)) {
|
||||||
|
|
||||||
$this->setProperties(null, $this->original['width'], $this->original['height']);
|
$this->setPropertiesEmpty($this->original['width'], $this->original['height']);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$this->setProperties($this->dirname .'/'. $this->basename);
|
$this->setPropertiesFromPath($this->dirname .'/'. $this->basename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@@ -15,6 +15,44 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
|||||||
$this->assertInstanceOf('Illuminate\Filesystem\Filesystem', $img->getFilesystem());
|
$this->assertInstanceOf('Illuminate\Filesystem\Filesystem', $img->getFilesystem());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testConstructor()
|
||||||
|
{
|
||||||
|
$img = new Image('public/test.jpg');
|
||||||
|
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||||
|
$this->assertInternalType('resource', $img->resource);
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
$this->assertEquals($img->width, 800);
|
||||||
|
$this->assertEquals($img->height, 600);
|
||||||
|
$this->assertEquals($img->dirname, 'public');
|
||||||
|
$this->assertEquals($img->basename, 'test.jpg');
|
||||||
|
$this->assertEquals($img->extension, 'jpg');
|
||||||
|
$this->assertEquals($img->filename, 'test');
|
||||||
|
|
||||||
|
$img = new Image(null, 800, 600);
|
||||||
|
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||||
|
$this->assertInternalType('resource', $img->resource);
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
$this->assertEquals($img->width, 800);
|
||||||
|
$this->assertEquals($img->height, 600);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOpenImage()
|
||||||
|
{
|
||||||
|
$img = new Image;
|
||||||
|
$img->open('public/test.jpg');
|
||||||
|
$this->assertInternalType('resource', $img->resource);
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
$this->assertEquals($img->width, 800);
|
||||||
|
$this->assertEquals($img->height, 600);
|
||||||
|
$this->assertEquals($img->dirname, 'public');
|
||||||
|
$this->assertEquals($img->basename, 'test.jpg');
|
||||||
|
$this->assertEquals($img->extension, 'jpg');
|
||||||
|
$this->assertEquals($img->filename, 'test');
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreationFromFile()
|
public function testCreationFromFile()
|
||||||
{
|
{
|
||||||
$img = $this->getTestImage();
|
$img = $this->getTestImage();
|
||||||
@@ -583,6 +621,17 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
|||||||
$this->assertEquals($img->height, 200);
|
$this->assertEquals($img->height, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testStaticCallRaw()
|
||||||
|
{
|
||||||
|
$data = file_get_contents('public/test.jpg');
|
||||||
|
$img = Image::raw($data);
|
||||||
|
$this->assertInternalType('resource', $img->resource);
|
||||||
|
$this->assertInternalType('int', $img->width);
|
||||||
|
$this->assertInternalType('int', $img->height);
|
||||||
|
$this->assertEquals($img->width, 800);
|
||||||
|
$this->assertEquals($img->height, 600);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCreateCanvasWithTransparentBackground()
|
public function testCreateCanvasWithTransparentBackground()
|
||||||
{
|
{
|
||||||
$img = Image::canvas(100, 100);
|
$img = Image::canvas(100, 100);
|
||||||
|
Reference in New Issue
Block a user