add support for Image creation from string image data
This commit is contained in:
parent
60e7f53945
commit
51c6b5fc38
@ -35,6 +35,7 @@ class Image extends AbstractElement
|
|||||||
const SOURCE_LOCAL = 'local'; // Local images
|
const SOURCE_LOCAL = 'local'; // Local images
|
||||||
const SOURCE_GD = 'gd'; // Generated using GD
|
const SOURCE_GD = 'gd'; // Generated using GD
|
||||||
const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image
|
const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image
|
||||||
|
const SOURCE_STRING = 'string'; // Image from string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image source
|
* Image source
|
||||||
@ -379,6 +380,8 @@ class Image extends AbstractElement
|
|||||||
// Check image data
|
// Check image data
|
||||||
if ($this->sourceType == self::SOURCE_ARCHIVE) {
|
if ($this->sourceType == self::SOURCE_ARCHIVE) {
|
||||||
$imageData = $this->getArchiveImageSize($source);
|
$imageData = $this->getArchiveImageSize($source);
|
||||||
|
} else if ($this->sourceType == self::SOURCE_STRING) {
|
||||||
|
$imageData = $this->getStringImageSize($source);
|
||||||
} else {
|
} else {
|
||||||
$imageData = @getimagesize($source);
|
$imageData = @getimagesize($source);
|
||||||
}
|
}
|
||||||
@ -416,9 +419,15 @@ class Image extends AbstractElement
|
|||||||
} elseif (strpos($source, 'zip://') !== false) {
|
} elseif (strpos($source, 'zip://') !== false) {
|
||||||
$this->memoryImage = false;
|
$this->memoryImage = false;
|
||||||
$this->sourceType = self::SOURCE_ARCHIVE;
|
$this->sourceType = self::SOURCE_ARCHIVE;
|
||||||
|
} elseif (filter_var($source, FILTER_VALIDATE_URL) !== false) {
|
||||||
|
$this->memoryImage = true;
|
||||||
|
$this->sourceType = self::SOURCE_GD;
|
||||||
|
} elseif (@file_exists($source)) {
|
||||||
|
$this->memoryImage = false;
|
||||||
|
$this->sourceType = self::SOURCE_LOCAL;
|
||||||
} else {
|
} else {
|
||||||
$this->memoryImage = (filter_var($source, FILTER_VALIDATE_URL) !== false);
|
$this->memoryImage = true;
|
||||||
$this->sourceType = $this->memoryImage ? self::SOURCE_GD : self::SOURCE_LOCAL;
|
$this->sourceType = self::SOURCE_STRING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,6 +469,24 @@ class Image extends AbstractElement
|
|||||||
return $imageData;
|
return $imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get image size from string
|
||||||
|
*
|
||||||
|
* @param string $source
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore this method is just a replacement for getimagesizefromstring which exists only as of PHP 5.4
|
||||||
|
*/
|
||||||
|
private function getStringImageSize($source)
|
||||||
|
{
|
||||||
|
if (!function_exists('getimagesizefromstring')) {
|
||||||
|
$uri = 'data://application/octet-stream;base64,' . base64_encode($source);
|
||||||
|
return @getimagesize($uri);
|
||||||
|
} else {
|
||||||
|
return @getimagesizefromstring($source);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set image functions and extensions.
|
* Set image functions and extensions.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -156,4 +156,57 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$image = new Image("zip://{$archiveFile}#{$imageFile}");
|
$image = new Image("zip://{$archiveFile}#{$imageFile}");
|
||||||
$this->assertEquals('image/jpeg', $image->getImageType());
|
$this->assertEquals('image/jpeg', $image->getImageType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting image as string
|
||||||
|
*/
|
||||||
|
public function testImageAsStringFromFile()
|
||||||
|
{
|
||||||
|
$image = new Image(__DIR__ . '/../_files/images/earth.jpg');
|
||||||
|
|
||||||
|
$this->assertNotNull($image->getImageStringData());
|
||||||
|
$this->assertNotNull($image->getImageStringData(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting image from zip as string
|
||||||
|
*/
|
||||||
|
public function testImageAsStringFromZip()
|
||||||
|
{
|
||||||
|
$archiveFile = __DIR__ . '/../_files/documents/reader.docx';
|
||||||
|
$imageFile = 'word/media/image1.jpeg';
|
||||||
|
$image = new Image("zip://{$archiveFile}#{$imageFile}");
|
||||||
|
|
||||||
|
$this->assertNotNull($image->getImageStringData());
|
||||||
|
$this->assertNotNull($image->getImageStringData(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test construct from string
|
||||||
|
*/
|
||||||
|
public function testConstructFromString()
|
||||||
|
{
|
||||||
|
$source = file_get_contents(__DIR__ . '/../_files/images/earth.jpg');
|
||||||
|
|
||||||
|
$image = new Image($source);
|
||||||
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);
|
||||||
|
$this->assertEquals($source, $image->getSource());
|
||||||
|
$this->assertEquals(md5($source), $image->getMediaId());
|
||||||
|
$this->assertEquals('image/jpeg', $image->getImageType());
|
||||||
|
$this->assertEquals('jpg', $image->getImageExtension());
|
||||||
|
$this->assertEquals('imagecreatefromjpeg', $image->getImageCreateFunction());
|
||||||
|
$this->assertEquals('imagejpeg', $image->getImageFunction());
|
||||||
|
$this->assertTrue($image->isMemImage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test invalid string image
|
||||||
|
*
|
||||||
|
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
|
||||||
|
*/
|
||||||
|
public function testInvalidImageString()
|
||||||
|
{
|
||||||
|
$object = new Image('this_is-a_non_valid_image');
|
||||||
|
$object->getSource();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user