mirror of
https://github.com/Intervention/image.git
synced 2025-08-16 19:04:00 +02:00
Adds ImageManagerBasic
This commit is contained in:
committed by
Oliver Vogel
parent
dd5a47ba16
commit
d3321a7c6f
108
src/Intervention/Image/ImageManagerBasic.php
Normal file
108
src/Intervention/Image/ImageManagerBasic.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
|
||||||
|
class ImageManagerBasic
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Config
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default config
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $defaultConfig = array(
|
||||||
|
'driver' => 'gd',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new instance of Image Manager
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
*/
|
||||||
|
public function __construct(array $config = array())
|
||||||
|
{
|
||||||
|
$this->config = $this->defaultConfig + $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a driver instance according to config settings
|
||||||
|
*
|
||||||
|
* @return Intervention\Image\AbstractDriver
|
||||||
|
*/
|
||||||
|
private function createDriver()
|
||||||
|
{
|
||||||
|
$drivername = ucfirst($this->config['driver']);
|
||||||
|
$driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername);
|
||||||
|
|
||||||
|
if (class_exists($driverclass)) {
|
||||||
|
return new $driverclass;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Intervention\Image\Exception\NotSupportedException(
|
||||||
|
"Driver ({$drivername}) could not be instantiated."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initiates an Image instance from different input types
|
||||||
|
*
|
||||||
|
* @param mixed $data
|
||||||
|
*
|
||||||
|
* @return Intervention\Image\Image
|
||||||
|
*/
|
||||||
|
public function make($data)
|
||||||
|
{
|
||||||
|
return $this->createDriver()->init($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty image canvas
|
||||||
|
*
|
||||||
|
* @param integer $width
|
||||||
|
* @param integer $height
|
||||||
|
* @param mixed $background
|
||||||
|
*
|
||||||
|
* @return Intervention\Image\Image
|
||||||
|
*/
|
||||||
|
public function canvas($width, $height, $background = null)
|
||||||
|
{
|
||||||
|
return $this->createDriver()->newImage($width, $height, $background);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new cached image and run callback
|
||||||
|
* (requires additional package intervention/imagecache)
|
||||||
|
*
|
||||||
|
* @param Closure $callback
|
||||||
|
* @param integer $lifetime
|
||||||
|
* @param boolean $returnObj
|
||||||
|
*
|
||||||
|
* @return Image
|
||||||
|
*/
|
||||||
|
public function cache(Closure $callback, $lifetime = null, $returnObj = false)
|
||||||
|
{
|
||||||
|
if (class_exists('Intervention\\Image\\ImageCache')) {
|
||||||
|
// create imagecache
|
||||||
|
$imagecache = new ImageCache($this);
|
||||||
|
|
||||||
|
// run callback
|
||||||
|
if (is_callable($callback)) {
|
||||||
|
$callback($imagecache);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $imagecache->get($lifetime, $returnObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Intervention\Image\Exception\NotSupportedException(
|
||||||
|
"Please install package intervention/imagecache before running this function."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -7,20 +7,20 @@ use Closure;
|
|||||||
class ImageManagerStatic
|
class ImageManagerStatic
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Instance of Intervention\Image\ImageManager
|
* Instance of Intervention\Image\ImageManagerBasic
|
||||||
*
|
*
|
||||||
* @var ImageManager
|
* @var ImageManagerBasic
|
||||||
*/
|
*/
|
||||||
public $manager;
|
public $manager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance
|
* Creates a new instance
|
||||||
*
|
*
|
||||||
* @param ImageManager $manager
|
* @param ImageManagerBasic $manager
|
||||||
*/
|
*/
|
||||||
public function __construct(ImageManager $manager = null)
|
public function __construct(ImageManagerBasic $manager = null)
|
||||||
{
|
{
|
||||||
$this->manager = $manager ? $manager : new ImageManager;
|
$this->manager = $manager ? $manager : new ImageManagerBasic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -11,7 +11,7 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testNewInstance()
|
public function testNewInstance()
|
||||||
{
|
{
|
||||||
$manager = Mockery::mock('Intervention\Image\ImageManager');
|
$manager = Mockery::mock('Intervention\Image\ImageManagerBasic');
|
||||||
$managerStatic = new ImageManagerStatic($manager);
|
$managerStatic = new ImageManagerStatic($manager);
|
||||||
$m = $managerStatic->newInstance();
|
$m = $managerStatic->newInstance();
|
||||||
$this->assertInstanceOf('Intervention\Image\ImageManagerStatic', $m);
|
$this->assertInstanceOf('Intervention\Image\ImageManagerStatic', $m);
|
||||||
@@ -19,7 +19,7 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testMake()
|
public function testMake()
|
||||||
{
|
{
|
||||||
$manager = Mockery::mock('Intervention\Image\ImageManager');
|
$manager = Mockery::mock('Intervention\Image\ImageManagerBasic');
|
||||||
$managerStatic = new ImageManagerStatic($manager);
|
$managerStatic = new ImageManagerStatic($manager);
|
||||||
$img = $managerStatic->make('tests/images/tile.png');
|
$img = $managerStatic->make('tests/images/tile.png');
|
||||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||||
@@ -27,7 +27,7 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testCanvas()
|
public function testCanvas()
|
||||||
{
|
{
|
||||||
$manager = Mockery::mock('Intervention\Image\ImageManager');
|
$manager = Mockery::mock('Intervention\Image\ImageManagerBasic');
|
||||||
$managerStatic = new ImageManagerStatic($manager);
|
$managerStatic = new ImageManagerStatic($manager);
|
||||||
$img = $managerStatic->canvas(100, 100);
|
$img = $managerStatic->canvas(100, 100);
|
||||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||||
|
Reference in New Issue
Block a user