PHPWord/tests/PhpWord/Element/ImageTest.php

224 lines
7.4 KiB
PHP
Raw Normal View History

2014-03-09 19:09:22 +01:00
<?php
2014-03-27 23:55:06 +07:00
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2014-03-27 23:55:06 +07:00
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-27 23:55:06 +07:00
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord\Element;
2014-03-09 19:09:22 +01:00
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
2014-03-09 19:09:22 +01:00
/**
* Test class for PhpOffice\PhpWord\Element\Image
2014-03-27 23:55:06 +07:00
*
* @runTestsInSeparateProcesses
*/
class ImageTest extends \PHPUnit\Framework\TestCase
2014-03-09 15:04:23 -04:00
{
2014-03-30 11:09:14 +07:00
/**
* New instance
*/
2014-03-09 15:04:23 -04:00
public function testConstruct()
{
$src = __DIR__ . '/../_files/images/firefox.png';
$oImage = new Image($src);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
$this->assertEquals($src, $oImage->getSource());
$this->assertEquals(md5($src), $oImage->getMediaId());
2017-11-22 09:43:35 +01:00
$this->assertFalse($oImage->isWatermark());
$this->assertEquals(Image::SOURCE_LOCAL, $oImage->getSourceType());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
2014-03-30 11:09:14 +07:00
/**
* New instance with style
*/
2014-03-09 15:04:23 -04:00
public function testConstructWithStyle()
{
$src = __DIR__ . '/../_files/images/firefox.png';
$oImage = new Image(
$src,
array(
'width' => 210,
'height' => 210,
2015-10-10 19:22:19 +04:00
'alignment' => Jc::CENTER,
'wrappingStyle' => \PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND,
)
);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
/**
2014-03-30 11:09:14 +07:00
* Valid image types
*/
public function testImages()
{
$images = array(
array('mars.jpg', 'image/jpeg', 'jpg', 'imagecreatefromjpeg', 'imagejpeg'),
array('mario.gif', 'image/gif', 'gif', 'imagecreatefromgif', 'imagegif'),
array('firefox.png', 'image/png', 'png', 'imagecreatefrompng', 'imagepng'),
array('duke_nukem.bmp', 'image/bmp', 'bmp', null, null),
array('angela_merkel.tif', 'image/tiff', 'tif', null, null),
);
foreach ($images as $imageData) {
list($source, $type, $extension, $createFunction, $imageFunction) = $imageData;
$source = __DIR__ . "/../_files/images/{$source}";
$image = new Image($source);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);
$this->assertEquals($source, $image->getSource());
$this->assertEquals(md5($source), $image->getMediaId());
$this->assertEquals($type, $image->getImageType());
$this->assertEquals($extension, $image->getImageExtension());
$this->assertEquals($createFunction, $image->getImageCreateFunction());
$this->assertEquals($imageFunction, $image->getImageFunction());
2014-05-04 15:13:31 +07:00
$this->assertFalse($image->isMemImage());
2017-11-05 21:39:10 +01:00
$this->assertNotNull($image->getImageStringData());
}
}
2014-03-30 11:09:14 +07:00
/**
* Get style
*/
2014-03-09 15:04:23 -04:00
public function testStyle()
{
2014-03-23 12:37:31 -04:00
$oImage = new Image(
__DIR__ . '/../_files/images/earth.jpg',
2015-10-10 19:22:19 +04:00
array('height' => 210, 'alignment' => Jc::CENTER)
2014-03-23 12:37:31 -04:00
);
2014-03-09 15:04:23 -04:00
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oImage->getStyle());
2014-03-09 15:04:23 -04:00
}
2014-04-12 10:23:31 +07:00
/**
* Test invalid local image
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
2014-03-30 11:09:14 +07:00
*/
public function testInvalidImageLocal()
{
new Image(__DIR__ . '/../_files/images/thisisnotarealimage');
}
2014-03-30 11:09:14 +07:00
/**
* Test invalid PHP Image
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
2014-03-30 11:09:14 +07:00
*/
public function testInvalidImagePhp()
{
$object = new Image('test.php');
$object->getSource();
}
2014-03-30 11:09:14 +07:00
/**
* Test unsupported image
*
* @expectedException \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
2014-03-30 11:09:14 +07:00
*/
public function testUnsupportedImage()
2014-03-30 11:09:14 +07:00
{
2017-01-29 13:16:54 +01:00
//disable ssl verification, never do this in real application, you should pass the certiciate instead!!!
$arrContextOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
2017-01-29 13:16:54 +01:00
),
);
stream_context_set_default($arrContextOptions);
$object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP');
$object->getSource();
2014-03-30 11:09:14 +07:00
}
2014-04-06 15:19:09 +07:00
/**
* Get relation Id
2014-04-06 15:19:09 +07:00
*/
public function testRelationID()
2014-04-06 15:19:09 +07:00
{
$oImage = new Image(__DIR__ . '/../_files/images/earth.jpg', array('width' => 100));
$iVal = rand(1, 1000);
$oImage->setRelationId($iVal);
$this->assertEquals($iVal, $oImage->getRelationId());
2014-04-06 15:19:09 +07:00
}
/**
* Test archived image
*/
public function testArchivedImage()
{
$archiveFile = __DIR__ . '/../_files/documents/reader.docx';
$imageFile = 'word/media/image1.jpeg';
$image = new Image("zip://{$archiveFile}#{$imageFile}");
$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());
2017-01-29 13:16:54 +01:00
$this->assertEquals('imagecreatefromstring', $image->getImageCreateFunction());
$this->assertEquals('imagejpeg', $image->getImageFunction());
$this->assertTrue($image->isMemImage());
2017-11-11 23:49:23 +01:00
$this->assertNotNull($image->getImageStringData());
$this->assertNotNull($image->getImageStringData(true));
}
/**
* Test invalid string image
*
* @expectedException \PhpOffice\PhpWord\Exception\InvalidImageException
*/
public function testInvalidImageString()
{
$object = new Image('this_is-a_non_valid_image');
$object->getSource();
}
}