PHPWord/samples/Sample_13_Images.php

103 lines
4.0 KiB
PHP
Raw Permalink Normal View History

<?php
2022-09-16 11:45:45 +02:00
2018-03-11 13:27:35 +01:00
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Shared\Converter;
2014-03-15 22:39:57 +07:00
include_once 'Sample_Header.php';
// New Word document
2015-01-01 20:41:42 +04:00
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Begin code
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addText('Local image without any styles:');
$section->addImage('resources/_mars.jpg');
2018-03-11 13:27:35 +01:00
printSeparator($section);
2016-06-04 20:06:37 +04:00
$section->addText('Local image with styles:');
2022-09-16 11:45:45 +02:00
$section->addImage('resources/_earth.jpg', ['width' => 210, 'height' => 210, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER]);
// Remote image
2018-03-11 13:27:35 +01:00
printSeparator($section);
$source = 'http://php.net/images/logos/php-med-trans-light.gif';
2016-06-04 20:06:37 +04:00
$section->addText("Remote image from: {$source}");
$section->addImage($source);
2017-01-29 13:16:54 +01:00
// Image from string
2018-03-11 13:27:35 +01:00
printSeparator($section);
2017-01-29 13:16:54 +01:00
$source = 'resources/_mars.jpg';
$fileContent = file_get_contents($source);
$section->addText('Image from string');
2017-01-29 13:16:54 +01:00
$section->addImage($fileContent);
//Wrapping style
2018-03-11 13:27:35 +01:00
printSeparator($section);
$text = str_repeat('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ', 2);
2022-09-16 11:45:45 +02:00
$wrappingStyles = ['inline', 'behind', 'infront', 'square', 'tight'];
foreach ($wrappingStyles as $wrappingStyle) {
2016-06-04 20:06:37 +04:00
$section->addText("Wrapping style {$wrappingStyle}");
2015-01-01 20:41:42 +04:00
$section->addImage(
'resources/_earth.jpg',
2022-09-16 11:45:45 +02:00
[
'positioning' => 'relative',
'marginTop' => -1,
'marginLeft' => 1,
'width' => 80,
'height' => 80,
'wrappingStyle' => $wrappingStyle,
'wrapDistanceRight' => Converter::cmToPoint(1),
2018-03-11 13:27:35 +01:00
'wrapDistanceBottom' => Converter::cmToPoint(1),
2022-09-16 11:45:45 +02:00
]
2015-01-01 20:41:42 +04:00
);
2016-06-04 20:06:37 +04:00
$section->addText($text);
2018-03-11 13:27:35 +01:00
printSeparator($section);
}
//Absolute positioning
2016-06-04 20:06:37 +04:00
$section->addText('Absolute positioning: see top right corner of page');
$section->addImage(
'resources/_mars.jpg',
2022-09-16 11:45:45 +02:00
[
'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT,
2014-05-21 23:53:08 +07:00
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
2022-09-16 11:45:45 +02:00
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(15.5),
'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.55),
]
);
//Relative positioning
2018-03-11 13:27:35 +01:00
printSeparator($section);
2016-06-04 20:06:37 +04:00
$section->addText('Relative positioning: Horizontal position center relative to column,');
$section->addText('Vertical position top relative to line');
$section->addImage(
'resources/_mars.jpg',
2022-09-16 11:45:45 +02:00
[
'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
2022-09-16 11:45:45 +02:00
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_LINE,
]
);
2022-09-16 11:45:45 +02:00
function printSeparator(Section $section): void
2018-03-11 13:27:35 +01:00
{
$section->addTextBreak();
2022-09-16 11:45:45 +02:00
$lineStyle = ['weight' => 0.2, 'width' => 150, 'height' => 0, 'align' => 'center'];
2018-03-11 13:27:35 +01:00
$section->addLine($lineStyle);
$section->addTextBreak(2);
}
// Save file
2014-04-12 10:12:24 +07:00
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}