Skip to content

Commit 217fd6e

Browse files
committed
fix image loading over https
1 parent 6da9d8a commit 217fd6e

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

samples/Sample_13_Images.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
$section->addText("Remote image from: {$source}");
2121
$section->addImage($source);
2222

23+
// Image from string
24+
$source = 'resources/_mars.jpg';
25+
$fileContent = file_get_contents($source);
26+
$section->addText("Image from string");
27+
$section->addImage($fileContent);
28+
2329
//Wrapping style
2430
$text = str_repeat('Hello World! ', 15);
2531
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');

src/PhpWord/Element/Image.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ public function getImageStringData($base64 = false)
340340
call_user_func($this->imageFunc, $imageResource);
341341
$imageBinary = ob_get_contents();
342342
ob_end_clean();
343+
} elseif ($this->sourceType == self::SOURCE_STRING) {
344+
$imageBinary = $this->source;
343345
} else {
344346
$fileHandle = fopen($actualSource, 'rb', false);
345347
if ($fileHandle !== false) {
@@ -366,33 +368,31 @@ public function getImageStringData($base64 = false)
366368
/**
367369
* Check memory image, supported type, image functions, and proportional width/height.
368370
*
369-
* @param string $source
370-
*
371371
* @return void
372372
*
373373
* @throws \PhpOffice\PhpWord\Exception\InvalidImageException
374374
* @throws \PhpOffice\PhpWord\Exception\UnsupportedImageTypeException
375375
*/
376-
private function checkImage($source)
376+
private function checkImage()
377377
{
378-
$this->setSourceType($source);
378+
$this->setSourceType();
379379

380380
// Check image data
381381
if ($this->sourceType == self::SOURCE_ARCHIVE) {
382-
$imageData = $this->getArchiveImageSize($source);
382+
$imageData = $this->getArchiveImageSize($this->source);
383383
} else if ($this->sourceType == self::SOURCE_STRING) {
384-
$imageData = $this->getStringImageSize($source);
384+
$imageData = $this->getStringImageSize($this->source);
385385
} else {
386-
$imageData = @getimagesize($source);
386+
$imageData = @getimagesize($this->source);
387387
}
388388
if (!is_array($imageData)) {
389-
throw new InvalidImageException(sprintf('Invalid image: %s', $source));
389+
throw new InvalidImageException(sprintf('Invalid image: %s', $this->source));
390390
}
391391
list($actualWidth, $actualHeight, $imageType) = $imageData;
392392

393393
// Check image type support
394394
$supportedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
395-
if ($this->sourceType != self::SOURCE_GD) {
395+
if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) {
396396
$supportedTypes = array_merge($supportedTypes, array(IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM));
397397
}
398398
if (!in_array($imageType, $supportedTypes)) {
@@ -408,21 +408,26 @@ private function checkImage($source)
408408
/**
409409
* Set source type.
410410
*
411-
* @param string $source
412411
* @return void
413412
*/
414-
private function setSourceType($source)
413+
private function setSourceType()
415414
{
416-
if (stripos(strrev($source), strrev('.php')) === 0) {
415+
if (stripos(strrev($this->source), strrev('.php')) === 0) {
417416
$this->memoryImage = true;
418417
$this->sourceType = self::SOURCE_GD;
419-
} elseif (strpos($source, 'zip://') !== false) {
418+
} elseif (strpos($this->source, 'zip://') !== false) {
420419
$this->memoryImage = false;
421420
$this->sourceType = self::SOURCE_ARCHIVE;
422-
} elseif (filter_var($source, FILTER_VALIDATE_URL) !== false) {
421+
} elseif (filter_var($this->source, FILTER_VALIDATE_URL) !== false) {
423422
$this->memoryImage = true;
424-
$this->sourceType = self::SOURCE_GD;
425-
} elseif (@file_exists($source)) {
423+
if (strpos($this->source, 'https') === 0) {
424+
$fileContent = file_get_contents($this->source);
425+
$this->source = $fileContent;
426+
$this->sourceType = self::SOURCE_STRING;
427+
} else {
428+
$this->sourceType = self::SOURCE_GD;
429+
}
430+
} elseif (@file_exists($this->source)) {
426431
$this->memoryImage = false;
427432
$this->sourceType = self::SOURCE_LOCAL;
428433
} else {
@@ -496,18 +501,18 @@ private function setFunctions()
496501
{
497502
switch ($this->imageType) {
498503
case 'image/png':
499-
$this->imageCreateFunc = 'imagecreatefrompng';
504+
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefrompng';
500505
$this->imageFunc = 'imagepng';
501506
$this->imageExtension = 'png';
502507
break;
503508
case 'image/gif':
504-
$this->imageCreateFunc = 'imagecreatefromgif';
509+
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromgif';
505510
$this->imageFunc = 'imagegif';
506511
$this->imageExtension = 'gif';
507512
break;
508513
case 'image/jpeg':
509514
case 'image/jpg':
510-
$this->imageCreateFunc = 'imagecreatefromjpeg';
515+
$this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromjpeg';
511516
$this->imageFunc = 'imagejpeg';
512517
$this->imageExtension = 'jpg';
513518
break;

tests/PhpWord/Element/ImageTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ public function testInvalidImagePhp()
131131
*/
132132
public function testUnsupportedImage()
133133
{
134-
$object = new Image('http://samples.libav.org/image-samples/RACECAR.BMP');
134+
//disable ssl verification, never do this in real application, you should pass the certiciate instead!!!
135+
$arrContextOptions = array(
136+
"ssl" => array(
137+
"verify_peer" => false,
138+
"verify_peer_name" => false,
139+
),
140+
);
141+
stream_context_set_default($arrContextOptions);
142+
$object = new Image('https://samples.libav.org/image-samples/RACECAR.BMP');
135143
$object->getSource();
136144
}
137145

@@ -194,7 +202,7 @@ public function testConstructFromString()
194202
$this->assertEquals(md5($source), $image->getMediaId());
195203
$this->assertEquals('image/jpeg', $image->getImageType());
196204
$this->assertEquals('jpg', $image->getImageExtension());
197-
$this->assertEquals('imagecreatefromjpeg', $image->getImageCreateFunction());
205+
$this->assertEquals('imagecreatefromstring', $image->getImageCreateFunction());
198206
$this->assertEquals('imagejpeg', $image->getImageFunction());
199207
$this->assertTrue($image->isMemImage());
200208
}

0 commit comments

Comments
 (0)