Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions samples/Sample_13_Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
$section->addText("Remote image from: {$source}");
$section->addImage($source);

// Image from string
$source = 'resources/_mars.jpg';
$fileContent = file_get_contents($source);
$section->addText("Image from string");
$section->addImage($fileContent);

//Wrapping style
$text = str_repeat('Hello World! ', 15);
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
Expand Down
43 changes: 24 additions & 19 deletions src/PhpWord/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ public function getImageStringData($base64 = false)
call_user_func($this->imageFunc, $imageResource);
$imageBinary = ob_get_contents();
ob_end_clean();
} elseif ($this->sourceType == self::SOURCE_STRING) {
$imageBinary = $this->source;
} else {
$fileHandle = fopen($actualSource, 'rb', false);
if ($fileHandle !== false) {
Expand All @@ -366,33 +368,31 @@ public function getImageStringData($base64 = false)
/**
* Check memory image, supported type, image functions, and proportional width/height.
*
* @param string $source
*
* @return void
*
* @throws \PhpOffice\PhpWord\Exception\InvalidImageException
* @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
*/
private function checkImage($source)
private function checkImage()
{
$this->setSourceType($source);
$this->setSourceType();

// Check image data
if ($this->sourceType == self::SOURCE_ARCHIVE) {
$imageData = $this->getArchiveImageSize($source);
$imageData = $this->getArchiveImageSize($this->source);
} else if ($this->sourceType == self::SOURCE_STRING) {
$imageData = $this->getStringImageSize($source);
$imageData = $this->getStringImageSize($this->source);
} else {
$imageData = @getimagesize($source);
$imageData = @getimagesize($this->source);
}
if (!is_array($imageData)) {
throw new InvalidImageException(sprintf('Invalid image: %s', $source));
throw new InvalidImageException(sprintf('Invalid image: %s', $this->source));
}
list($actualWidth, $actualHeight, $imageType) = $imageData;

// Check image type support
$supportedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
if ($this->sourceType != self::SOURCE_GD) {
if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) {
$supportedTypes = array_merge($supportedTypes, array(IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM));
}
if (!in_array($imageType, $supportedTypes)) {
Expand All @@ -408,21 +408,26 @@ private function checkImage($source)
/**
* Set source type.
*
* @param string $source
* @return void
*/
private function setSourceType($source)
private function setSourceType()
{
if (stripos(strrev($source), strrev('.php')) === 0) {
if (stripos(strrev($this->source), strrev('.php')) === 0) {
$this->memoryImage = true;
$this->sourceType = self::SOURCE_GD;
} elseif (strpos($source, 'zip://') !== false) {
} elseif (strpos($this->source, 'zip://') !== false) {
$this->memoryImage = false;
$this->sourceType = self::SOURCE_ARCHIVE;
} elseif (filter_var($source, FILTER_VALIDATE_URL) !== false) {
} elseif (filter_var($this->source, FILTER_VALIDATE_URL) !== false) {
$this->memoryImage = true;
$this->sourceType = self::SOURCE_GD;
} elseif (@file_exists($source)) {
if (strpos($this->source, 'https') === 0) {
$fileContent = file_get_contents($this->source);
$this->source = $fileContent;
$this->sourceType = self::SOURCE_STRING;
} else {
$this->sourceType = self::SOURCE_GD;
}
} elseif (@file_exists($this->source)) {
$this->memoryImage = false;
$this->sourceType = self::SOURCE_LOCAL;
} else {
Expand Down Expand Up @@ -496,18 +501,18 @@ private function setFunctions()
{
switch ($this->imageType) {
case 'image/png':
$this->imageCreateFunc = 'imagecreatefrompng';
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefrompng';
$this->imageFunc = 'imagepng';
$this->imageExtension = 'png';
break;
case 'image/gif':
$this->imageCreateFunc = 'imagecreatefromgif';
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromgif';
$this->imageFunc = 'imagegif';
$this->imageExtension = 'gif';
break;
case 'image/jpeg':
case 'image/jpg':
$this->imageCreateFunc = 'imagecreatefromjpeg';
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromjpeg';
$this->imageFunc = 'imagejpeg';
$this->imageExtension = 'jpg';
break;
Expand Down
12 changes: 10 additions & 2 deletions tests/PhpWord/Element/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ public function testInvalidImagePhp()
*/
public function testUnsupportedImage()
{
$object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP');
//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,
),
);
stream_context_set_default($arrContextOptions);
$object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP');
$object->getSource();
}

Expand Down Expand Up @@ -194,7 +202,7 @@ public function testConstructFromString()
$this->assertEquals(md5($source), $image->getMediaId());
$this->assertEquals('image/jpeg', $image->getImageType());
$this->assertEquals('jpg', $image->getImageExtension());
$this->assertEquals('imagecreatefromjpeg', $image->getImageCreateFunction());
$this->assertEquals('imagecreatefromstring', $image->getImageCreateFunction());
$this->assertEquals('imagejpeg', $image->getImageFunction());
$this->assertTrue($image->isMemImage());
}
Expand Down