Skip to content

Commit ff6b2a9

Browse files
committed
MemoryImage: Allow remote image when allow_url_open = on
1 parent 1aa83b9 commit ff6b2a9

File tree

10 files changed

+182
-3
lines changed

10 files changed

+182
-3
lines changed

Classes/PHPWord/Media.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
8686
$file = null;
8787
if ($type === 'image') {
8888
$cImg++;
89+
//Detect if it's a memory image first by php ext and second by regex
8990
$isMemImage = false;
9091
if (stripos(strrev($src), strrev('.php')) === 0) {
9192
$isMemImage = true;
9293
}
93-
94+
if (!$isMemImage) {
95+
$isMemImage = (filter_var($src, FILTER_VALIDATE_URL) !== false);
96+
}
9497
$extension = '';
9598
if ($isMemImage) {
9699
$extension = $memoryImage->getImageExtension();

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,9 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
751751

752752
/**
753753
* @param \PHPWord_Shared_XMLWriter $objWriter
754-
* @param \PHPWord_Section_Image $image
754+
* @param \PHPWord_Section_Image|\PHPWord_Section_MemoryImage $image
755755
*/
756-
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
756+
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $withoutP = false)
757757
{
758758
$rId = $image->getRelationId();
759759

Tests/PHPWord/SettingsTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPWord_Settings;
5+
6+
/**
7+
* Class TOCTest
8+
*
9+
* @package PHPWord\Tests
10+
* @covers PHPWord_Settings
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class SettingsTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @covers PHPWord_Settings::setCompatibility
17+
* @covers PHPWord_Settings::getCompatibility
18+
*/
19+
public function testGetSetCompatibility()
20+
{
21+
$this->assertTrue(PHPWord_Settings::getCompatibility());
22+
$this->assertTrue(PHPWord_Settings::setCompatibility(false));
23+
$this->assertFalse(PHPWord_Settings::getCompatibility());
24+
$this->assertFalse(PHPWord_Settings::setCompatibility('Non boolean'));
25+
}
26+
}

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Changes in branch for release 0.7.1 :
5151
- Feature: (ivanlanin) - Paragraph: setTabs() function
5252
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
5353
- Feature: (ivanlanin) - Reader: Initial effort for Word2007
54+
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
5455
- QA: (Progi1984) - UnitTests
5556

5657
Changes in branch for release 0.7.0 :

samples/Sample_00.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Generic template for creating PHPWord samples
4+
*/
5+
6+
// Init
7+
error_reporting(E_ALL);
8+
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
9+
require_once '../Classes/PHPWord.php';
10+
11+
// New Word document
12+
echo date('H:i:s'), " Create new PHPWord object", EOL;
13+
$PHPWord = new PHPWord();
14+
15+
// Begin code
16+
17+
18+
19+
// End code
20+
21+
// Save file
22+
$name = basename(__FILE__, '.php');
23+
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
24+
foreach ($writers as $writer => $extension) {
25+
echo date('H:i:s'), " Write to {$writer} format", EOL;
26+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
27+
$objWriter->save("{$name}.{$extension}");
28+
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
29+
}
30+
31+
// Done
32+
echo date('H:i:s'), " Done writing file(s)", EOL;
33+
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;

samples/Sample_12_HeaderFooter.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
// Init
3+
error_reporting(E_ALL);
4+
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
5+
require_once '../Classes/PHPWord.php';
6+
7+
// New Word document
8+
echo date('H:i:s') , " Create new PHPWord object" , EOL;
9+
$PHPWord = new PHPWord();
10+
11+
// New portrait section
12+
$section = $PHPWord->createSection();
13+
14+
// Add first page header
15+
$header = $section->createHeader();
16+
$header->firstPage();
17+
$table = $header->addTable();
18+
$table->addRow();
19+
$table->addCell(4500)->addText('This is the header.');
20+
$table->addCell(4500)->addImage(
21+
'resources/PHPWord.png',
22+
array('width' => 80, 'height' => 80, 'align' => 'right')
23+
);
24+
25+
// Add header for all other pages
26+
$subsequent = $section->createHeader();
27+
$subsequent->addText("Subsequent pages in Section 1 will Have this!");
28+
29+
// Add footer
30+
$footer = $section->createFooter();
31+
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align' => 'center'));
32+
33+
// Write some text
34+
$section->addTextBreak();
35+
$section->addText('Some text...');
36+
37+
// Create a second page
38+
$section->addPageBreak();
39+
40+
// Write some text
41+
$section->addTextBreak();
42+
$section->addText('Some text...');
43+
44+
// Create a third page
45+
$section->addPageBreak();
46+
47+
// Write some text
48+
$section->addTextBreak();
49+
$section->addText('Some text...');
50+
51+
// New portrait section
52+
$section2 = $PHPWord->createSection();
53+
54+
$sec2Header = $section2->createHeader();
55+
$sec2Header->addText("All pages in Section 2 will Have this!");
56+
57+
// Write some text
58+
$section2->addTextBreak();
59+
$section2->addText('Some text...');
60+
61+
62+
// Save file
63+
$name = basename(__FILE__, '.php');
64+
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
65+
foreach ($writers as $writer => $extension) {
66+
echo date('H:i:s'), " Write to {$writer} format", EOL;
67+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
68+
$objWriter->save("{$name}.{$extension}");
69+
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
70+
}
71+
72+
// Done
73+
echo date('H:i:s'), " Done writing file(s)", EOL;
74+
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;

samples/Sample_13_Images.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Image creation
4+
*/
5+
6+
// Init
7+
error_reporting(E_ALL);
8+
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
9+
require_once '../Classes/PHPWord.php';
10+
11+
// New Word document
12+
echo date('H:i:s'), " Create new PHPWord object", EOL;
13+
$PHPWord = new PHPWord();
14+
15+
// Begin code
16+
$section = $PHPWord->createSection();
17+
$section->addText('Local image without any styles:');
18+
$section->addImage('resources/_mars.jpg');
19+
$section->addTextBreak(2);
20+
//
21+
$section->addText('Local image with styles:');
22+
$section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center'));
23+
$section->addTextBreak(2);
24+
25+
$source = 'http://php.net/images/logos/php-med-trans-light.gif';
26+
$section->addText("Remote image from: {$source}");
27+
$section->addMemoryImage($source);
28+
// End code
29+
30+
// Save file
31+
$name = basename(__FILE__, '.php');
32+
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
33+
foreach ($writers as $writer => $extension) {
34+
echo date('H:i:s'), " Write to {$writer} format", EOL;
35+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
36+
$objWriter->save("{$name}.{$extension}");
37+
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
38+
}
39+
40+
// Done
41+
echo date('H:i:s'), " Done writing file(s)", EOL;
42+
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;

samples/resources/PHPWord.png

16.3 KB
Loading

samples/resources/_earth.jpg

36.8 KB
Loading

samples/resources/_mars.jpg

23.9 KB
Loading

0 commit comments

Comments
 (0)