mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 16:50:07 +02:00
added method encode
This commit is contained in:
@@ -68,7 +68,7 @@ class Image
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $mimetype;
|
||||
public $mime;
|
||||
|
||||
/**
|
||||
* Attributes of the original created image
|
||||
@@ -195,7 +195,7 @@ class Image
|
||||
$this->width = $info[0];
|
||||
$this->height = $info[1];
|
||||
$this->type = $info[2];
|
||||
$this->mimetype = $info['mime'];
|
||||
$this->mime = $info['mime'];
|
||||
|
||||
// set resource
|
||||
switch ($this->type) {
|
||||
@@ -1130,21 +1130,27 @@ class Image
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns image type stream
|
||||
* Encode image in different formats
|
||||
*
|
||||
* @param string $type gif|png|jpg|jpeg
|
||||
* @param integer quality
|
||||
* @param string $format
|
||||
* @param integer $quality
|
||||
* @return string
|
||||
*/
|
||||
private function data($type = null, $quality = 90)
|
||||
public function encode($format = null, $quality = 90)
|
||||
{
|
||||
$format = is_null($format) ? $this->type : $format;
|
||||
|
||||
if ($quality < 0 || $quality > 100) {
|
||||
throw new Exception('Quality of image must range from 0 to 100.');
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
switch (strtolower($type)) {
|
||||
switch (strtolower($format)) {
|
||||
case 'data-url':
|
||||
echo sprintf('data:%s;base64,%s', $this->mime, base64_encode($this->encode($this->type, $quality)));
|
||||
break;
|
||||
|
||||
case 'gif':
|
||||
case 1:
|
||||
@imagegif($this->resource);
|
||||
@@ -1291,7 +1297,7 @@ class Image
|
||||
public function save($path = null, $quality = 90)
|
||||
{
|
||||
$path = is_null($path) ? ($this->dirname .'/'. $this->basename) : $path;
|
||||
file_put_contents($path, $this->data(pathinfo($path, PATHINFO_EXTENSION), $quality));
|
||||
file_put_contents($path, $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality));
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -1363,6 +1369,6 @@ class Image
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->data();
|
||||
return $this->encode();
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,16 @@ use Intervention\Image\Image;
|
||||
|
||||
class ImageTest extends PHPUnit_Framework_Testcase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function getTestImage()
|
||||
{
|
||||
return new Image('public/test.jpg');
|
||||
@@ -22,7 +32,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->basename, 'test.jpg');
|
||||
$this->assertEquals($img->extension, 'jpg');
|
||||
$this->assertEquals($img->filename, 'test');
|
||||
$this->assertEquals($img->mimetype, 'image/jpeg');
|
||||
$this->assertEquals($img->mime, 'image/jpeg');
|
||||
|
||||
$img = new Image(null, 800, 600);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
@@ -46,7 +56,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->basename, 'test.jpg');
|
||||
$this->assertEquals($img->extension, 'jpg');
|
||||
$this->assertEquals($img->filename, 'test');
|
||||
$this->assertEquals($img->mimetype, 'image/jpeg');
|
||||
$this->assertEquals($img->mime, 'image/jpeg');
|
||||
}
|
||||
|
||||
public function testCreationFromFile()
|
||||
@@ -61,7 +71,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->basename, 'test.jpg');
|
||||
$this->assertEquals($img->extension, 'jpg');
|
||||
$this->assertEquals($img->filename, 'test');
|
||||
$this->assertEquals($img->mimetype, 'image/jpeg');
|
||||
$this->assertEquals($img->mime, 'image/jpeg');
|
||||
}
|
||||
|
||||
public function testResizeImage()
|
||||
@@ -1077,7 +1087,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($img->basename, 'test.jpg');
|
||||
$this->assertEquals($img->extension, 'jpg');
|
||||
$this->assertEquals($img->filename, 'test');
|
||||
$this->assertEquals($img->mimetype, 'image/jpeg');
|
||||
$this->assertEquals($img->mime, 'image/jpeg');
|
||||
}
|
||||
|
||||
public function testStaticCallCanvas()
|
||||
@@ -1122,4 +1132,24 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertEquals($color['blue'], 0);
|
||||
$this->assertEquals($color['alpha'], 127);
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
// default encoding
|
||||
$data = Image::make('public/circle.png')->encode();
|
||||
$this->assertInternalType('resource', @imagecreatefromstring($data));
|
||||
|
||||
// jpg encoding
|
||||
$data = Image::make('public/circle.png')->encode('jpg');
|
||||
$this->assertInternalType('resource', @imagecreatefromstring($data));
|
||||
|
||||
// gif encoding
|
||||
$data = Image::make('public/circle.png')->encode('gif');
|
||||
$this->assertInternalType('resource', @imagecreatefromstring($data));
|
||||
|
||||
// data-url encoding
|
||||
$data = Image::make('public/circle.png')->encode('data-url');
|
||||
$encoded = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAChklEQVRo3uXavUscQRjH8c+t2MhhcUhiKolYiDFXCIKFNkoqrYL/gyT/UV4sTZ3CgI2SIhBBsJBwWKjEYCEptLmzSpFi5ny56Hm+nLdjvt3e7t4+v52ZnWee3xTcHyMYxiAG0I8SivF8FUc4xD72sI3KfTy8cMf7y5jAOMZu+R+b2MA6th5ayCSmMXPujd+VKlaxhm/tFjKKOcyiR3s4wRcs40erN3Xd4AHzeIMpdGsf3XiBl/G4cl9CnmABb+PgfShK8aUVsYvaXYQMxlZ4rXOU0YefOL6NkLqIVzrPUBSze5WYribdKS8i6jxHb/wA1FoVstDh7tSsZQr43oqQ+Tiw80pZyBAqzYSMxi5VyrEQeCakN7/rP2QNF8zF5ss7QzFWlwmZFGbsVJiNMf8jZFr70o520BNjviCkLCSAqTETYz8VMuH+stiHpBhjPxUynqAI52PPhJXdWMJCxjCSCcvT1BnOhOQwdQYzoVCQOgOZUO1Inf5M/vOqVihl0pw/Gilmj0AEwjxSfQQ6qpmwSEmdo0yoxabOYSYUlFNnPxOq4qmzlwlr39TZzoRqxGbCIjZRqc8jGwkL2eBsYbUuzfmkGmM/FbIlmCypsRpjv1BFWRNMllQ4iTHjYqXxF54KJksKfMan+kFj0riMnQRE7MRYXdYinNVSp3Iu5B2+NhNCmFeKYuErhyxhsfHHq/yRXcEhyltBewUf3MDoqQmeXZ/gFOWBNbzHwWUnm3mIx7FlenPQMitRxJUJ7nWu7rHg2RU6OGaWYnc6aHZRKz57TfDsjgSn6KGqLjvC12nRNR57q0LqVISU/08cN+3a/XAiTHYfNXxim/HfbqppJPltTpfR0Y1nfwGRl30LQuetpgAAAABJRU5ErkJggg==';
|
||||
$this->assertEquals($data, $encoded);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user