Ei kuvausta

Sample_13_Images.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. include_once 'Sample_Header.php';
  3. // New Word document
  4. echo date('H:i:s'), ' Create new PhpWord object', EOL;
  5. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  6. // Begin code
  7. $section = $phpWord->addSection();
  8. $section->addText(htmlspecialchars('Local image without any styles:'));
  9. $section->addImage('resources/_mars.jpg');
  10. $section->addTextBreak(2);
  11. $section->addText(htmlspecialchars('Local image with styles:'));
  12. $section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center'));
  13. $section->addTextBreak(2);
  14. // Remote image
  15. $source = 'http://php.net/images/logos/php-med-trans-light.gif';
  16. $section->addText(htmlspecialchars("Remote image from: {$source}"));
  17. $section->addImage($source);
  18. //Wrapping style
  19. $text = str_repeat('Hello World! ', 15);
  20. $wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
  21. foreach ($wrappingStyles as $wrappingStyle) {
  22. $section->addTextBreak(5);
  23. $section->addText(htmlspecialchars("Wrapping style {$wrappingStyle}"));
  24. $section->addImage(
  25. 'resources/_earth.jpg',
  26. array(
  27. 'positioning' => 'relative',
  28. 'marginTop' => -1,
  29. 'marginLeft' => 1,
  30. 'width' => 80,
  31. 'height' => 80,
  32. 'wrappingStyle' => $wrappingStyle,
  33. )
  34. );
  35. $section->addText(htmlspecialchars($text));
  36. }
  37. //Absolute positioning
  38. $section->addTextBreak(3);
  39. $section->addText(htmlspecialchars('Absolute positioning: see top right corner of page'));
  40. $section->addImage(
  41. 'resources/_mars.jpg',
  42. array(
  43. 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
  44. 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
  45. 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
  46. 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT,
  47. 'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
  48. 'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE,
  49. 'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(15.5),
  50. 'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(1.55),
  51. )
  52. );
  53. //Relative positioning
  54. $section->addTextBreak(3);
  55. $section->addText(htmlspecialchars('Relative positioning: Horizontal position center relative to column,'));
  56. $section->addText(htmlspecialchars('Vertical position top relative to line'));
  57. $section->addImage(
  58. 'resources/_mars.jpg',
  59. array(
  60. 'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
  61. 'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(3),
  62. 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE,
  63. 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
  64. 'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
  65. 'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
  66. 'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_LINE,
  67. )
  68. );
  69. // Save file
  70. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  71. if (!CLI) {
  72. include_once 'Sample_Footer.php';
  73. }