Use exif_imagetype to check image format instead of extension name
This commit is contained in:
parent
9c0e70a198
commit
492f88d1e8
@ -30,15 +30,16 @@
|
|||||||
*/
|
*/
|
||||||
class PHPWord_Media
|
class PHPWord_Media
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Section Media Elements
|
* Section Media Elements
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static $_sectionMedia = array('images' => array(),
|
private static $_sectionMedia = array(
|
||||||
|
'images' => array(),
|
||||||
'embeddings' => array(),
|
'embeddings' => array(),
|
||||||
'links' => array());
|
'links' => array()
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Header Media Elements
|
* Header Media Elements
|
||||||
@ -61,18 +62,18 @@ class PHPWord_Media
|
|||||||
*/
|
*/
|
||||||
private static $_objectId = 1325353440;
|
private static $_objectId = 1325353440;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add new Section Media Element
|
* Add new Section Media Element
|
||||||
*
|
*
|
||||||
* @param string $src
|
* @param string $src
|
||||||
* @param string $type
|
* @param string $type
|
||||||
|
* @param PHPWord_Section_MemoryImage|null $memoryImage
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null)
|
public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null)
|
||||||
{
|
{
|
||||||
$mediaId = md5($src);
|
$mediaId = md5($src);
|
||||||
$key = ($type == 'image') ? 'images' : 'embeddings';
|
$key = ($type === 'image') ? 'images' : 'embeddings';
|
||||||
|
|
||||||
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
|
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
|
||||||
$cImg = self::countSectionMediaElements('images');
|
$cImg = self::countSectionMediaElements('images');
|
||||||
@ -81,26 +82,39 @@ class PHPWord_Media
|
|||||||
|
|
||||||
$media = array();
|
$media = array();
|
||||||
|
|
||||||
if ($type == 'image') {
|
$folder = null;
|
||||||
|
$file = null;
|
||||||
|
if ($type === 'image') {
|
||||||
$cImg++;
|
$cImg++;
|
||||||
$inf = pathinfo($src);
|
$isMemImage = false;
|
||||||
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false;
|
if (stripos(strrev($src), strrev('.php')) === 0) {
|
||||||
|
$isMemImage = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$extension = '';
|
||||||
if ($isMemImage) {
|
if ($isMemImage) {
|
||||||
$ext = $memoryImage->getImageExtension();
|
$extension = $memoryImage->getImageExtension();
|
||||||
$media['isMemImage'] = true;
|
$media['isMemImage'] = true;
|
||||||
$media['createfunction'] = $memoryImage->getImageCreateFunction();
|
$media['createfunction'] = $memoryImage->getImageCreateFunction();
|
||||||
$media['imagefunction'] = $memoryImage->getImageFunction();
|
$media['imagefunction'] = $memoryImage->getImageFunction();
|
||||||
} else {
|
} else {
|
||||||
$ext = $inf['extension'];
|
$imageType = exif_imagetype($src);
|
||||||
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
|
if ($imageType === IMAGETYPE_JPEG) {
|
||||||
$ext = 'jpg';
|
$extension = 'jpg';
|
||||||
|
} elseif ($imageType === IMAGETYPE_GIF) {
|
||||||
|
$extension = 'gif';
|
||||||
|
} elseif ($imageType === IMAGETYPE_PNG) {
|
||||||
|
$extension = 'png';
|
||||||
|
} elseif ($imageType === IMAGETYPE_BMP) {
|
||||||
|
$extension = 'bmp';
|
||||||
|
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
|
||||||
|
$extension = 'tif';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$folder = 'media';
|
$folder = 'media';
|
||||||
$file = $type . $cImg . '.' . strtolower($ext);
|
$file = $type . $cImg . '.' . strtolower($extension);
|
||||||
} elseif ($type == 'oleObject') {
|
} elseif ($type === 'oleObject') {
|
||||||
$cObj++;
|
$cObj++;
|
||||||
$folder = 'embedding';
|
$folder = 'embedding';
|
||||||
$file = $type . $cObj . '.bin';
|
$file = $type . $cObj . '.bin';
|
||||||
@ -113,27 +127,24 @@ class PHPWord_Media
|
|||||||
|
|
||||||
self::$_sectionMedia[$key][$mediaId] = $media;
|
self::$_sectionMedia[$key][$mediaId] = $media;
|
||||||
|
|
||||||
if ($type == 'oleObject') {
|
if ($type === 'oleObject') {
|
||||||
return array($rID, ++self::$_objectId);
|
return array($rID, ++self::$_objectId);
|
||||||
} else {
|
|
||||||
return $rID;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($type == 'oleObject') {
|
|
||||||
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
|
|
||||||
return array($rID, ++self::$_objectId);
|
|
||||||
} else {
|
|
||||||
return self::$_sectionMedia[$key][$mediaId]['rID'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $rID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($type === 'oleObject') {
|
||||||
|
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
|
||||||
|
return array($rID, ++self::$_objectId);
|
||||||
|
}
|
||||||
|
return self::$_sectionMedia[$key][$mediaId]['rID'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add new Section Link Element
|
* Add new Section Link Element
|
||||||
*
|
*
|
||||||
* @param string $linkSrc
|
* @param string $linkSrc
|
||||||
* @param string $linkName
|
|
||||||
*
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function addSectionLinkElement($linkSrc)
|
public static function addSectionLinkElement($linkSrc)
|
||||||
@ -160,12 +171,12 @@ class PHPWord_Media
|
|||||||
{
|
{
|
||||||
if (!is_null($key)) {
|
if (!is_null($key)) {
|
||||||
return self::$_sectionMedia[$key];
|
return self::$_sectionMedia[$key];
|
||||||
} else {
|
|
||||||
$arrImages = self::$_sectionMedia['images'];
|
|
||||||
$arrObjects = self::$_sectionMedia['embeddings'];
|
|
||||||
$arrLinks = self::$_sectionMedia['links'];
|
|
||||||
return array_merge($arrImages, $arrObjects, $arrLinks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$arrImages = self::$_sectionMedia['images'];
|
||||||
|
$arrObjects = self::$_sectionMedia['embeddings'];
|
||||||
|
$arrLinks = self::$_sectionMedia['links'];
|
||||||
|
return array_merge($arrImages, $arrObjects, $arrLinks);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -178,12 +189,12 @@ class PHPWord_Media
|
|||||||
{
|
{
|
||||||
if (!is_null($key)) {
|
if (!is_null($key)) {
|
||||||
return count(self::$_sectionMedia[$key]);
|
return count(self::$_sectionMedia[$key]);
|
||||||
} else {
|
|
||||||
$cImages = count(self::$_sectionMedia['images']);
|
|
||||||
$cObjects = count(self::$_sectionMedia['embeddings']);
|
|
||||||
$cLinks = count(self::$_sectionMedia['links']);
|
|
||||||
return ($cImages + $cObjects + $cLinks);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cImages = count(self::$_sectionMedia['images']);
|
||||||
|
$cObjects = count(self::$_sectionMedia['embeddings']);
|
||||||
|
$cLinks = count(self::$_sectionMedia['links']);
|
||||||
|
return ($cImages + $cObjects + $cLinks);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -191,6 +202,7 @@ class PHPWord_Media
|
|||||||
*
|
*
|
||||||
* @param int $headerCount
|
* @param int $headerCount
|
||||||
* @param string $src
|
* @param string $src
|
||||||
|
* @param PHPWord_Section_MemoryImage|null $memoryImage
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
|
public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
|
||||||
@ -232,9 +244,8 @@ class PHPWord_Media
|
|||||||
self::$_headerMedia[$key][$mediaId] = $media;
|
self::$_headerMedia[$key][$mediaId] = $media;
|
||||||
|
|
||||||
return $rID;
|
return $rID;
|
||||||
} else {
|
|
||||||
return self::$_headerMedia[$key][$mediaId]['rID'];
|
|
||||||
}
|
}
|
||||||
|
return self::$_headerMedia[$key][$mediaId]['rID'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,6 +274,7 @@ class PHPWord_Media
|
|||||||
*
|
*
|
||||||
* @param int $footerCount
|
* @param int $footerCount
|
||||||
* @param string $src
|
* @param string $src
|
||||||
|
* @param PHPWord_Section_MemoryImage|null $memoryImage
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
|
public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null)
|
||||||
@ -304,9 +316,8 @@ class PHPWord_Media
|
|||||||
self::$_footerMedia[$key][$mediaId] = $media;
|
self::$_footerMedia[$key][$mediaId] = $media;
|
||||||
|
|
||||||
return $rID;
|
return $rID;
|
||||||
} else {
|
|
||||||
return self::$_footerMedia[$key][$mediaId]['rID'];
|
|
||||||
}
|
}
|
||||||
|
return self::$_footerMedia[$key][$mediaId]['rID'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -30,7 +30,6 @@
|
|||||||
*/
|
*/
|
||||||
class PHPWord_Section_MemoryImage
|
class PHPWord_Section_MemoryImage
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image Src
|
* Image Src
|
||||||
*
|
*
|
||||||
@ -85,7 +84,7 @@ class PHPWord_Section_MemoryImage
|
|||||||
* Create a new Image
|
* Create a new Image
|
||||||
*
|
*
|
||||||
* @param string $src
|
* @param string $src
|
||||||
* @param mixed style
|
* @param mixed $style
|
||||||
*/
|
*/
|
||||||
public function __construct($src, $style = null)
|
public function __construct($src, $style = null)
|
||||||
{
|
{
|
||||||
@ -113,10 +112,6 @@ class PHPWord_Section_MemoryImage
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->_setFunctions();
|
$this->_setFunctions();
|
||||||
|
|
||||||
return $this;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +140,6 @@ class PHPWord_Section_MemoryImage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Image style
|
* Get Image style
|
||||||
*
|
*
|
||||||
@ -235,4 +229,4 @@ class PHPWord_Section_MemoryImage
|
|||||||
{
|
{
|
||||||
return $this->_imageExtension;
|
return $this->_imageExtension;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ namespace PHPWord\Tests;
|
|||||||
|
|
||||||
use PHPUnit_Framework_TestCase;
|
use PHPUnit_Framework_TestCase;
|
||||||
use PHPWord_Media;
|
use PHPWord_Media;
|
||||||
|
use PHPWord_Section;
|
||||||
|
|
||||||
class MediaTest extends \PHPUnit_Framework_TestCase
|
class MediaTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -25,4 +26,26 @@ class MediaTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media');
|
$this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Todo: add memory image to this test
|
||||||
|
*
|
||||||
|
* @covers PHPWord_Media::addSectionMediaElement
|
||||||
|
*/
|
||||||
|
public function testAddSectionMediaElement()
|
||||||
|
{
|
||||||
|
$section = new PHPWord_Section(0);
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
|
||||||
|
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
|
||||||
|
|
||||||
|
$elements = $section->getElements();
|
||||||
|
$this->assertEquals(6, count($elements));
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$this->assertInstanceOf('PHPWord_Section_Image', $element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -37,6 +37,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
|
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers PHPWord_Section_Image::__construct
|
||||||
|
*/
|
||||||
public function testValidImageTypes()
|
public function testValidImageTypes()
|
||||||
{
|
{
|
||||||
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
|
||||||
@ -49,6 +52,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
|
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
|
||||||
|
* @covers PHPWord_Section_Image::__construct
|
||||||
*/
|
*/
|
||||||
public function testImageNotFound()
|
public function testImageNotFound()
|
||||||
{
|
{
|
||||||
@ -57,6 +61,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
|
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
|
||||||
|
* @covers PHPWord_Section_Image::__construct
|
||||||
*/
|
*/
|
||||||
public function testInvalidImageTypes()
|
public function testInvalidImageTypes()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -16,8 +16,8 @@ use PHPWord\Tests\TestHelperDOCX;
|
|||||||
class TabsTest extends \PHPUnit_Framework_TestCase
|
class TabsTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Executed before each method of the class
|
* Executed before each method of the class
|
||||||
*/
|
*/
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
{
|
{
|
||||||
TestHelperDOCX::clear();
|
TestHelperDOCX::clear();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user