Skip to content

Commit 45ad431

Browse files
committed
Add images to templates
Image placeholders have the form ${img:<name>}. The width and height of the image to insert may be specified as additional arguments to the placeholder: ${img:<name>:width:height} For the width and height, CSS units cm, mm, px, pt and pc may be used, e.g. ${img:beispiel:15cm:360pt}
1 parent 4decaff commit 45ad431

File tree

5 files changed

+664
-1
lines changed

5 files changed

+664
-1
lines changed

src/PhpWord/Shared/Converter.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Shared;
1919

20+
use PhpOffice\PhpWord\Exception\InvalidStyleException;
21+
2022
/**
2123
* Common converter functions
2224
*/
@@ -227,6 +229,121 @@ public static function emuToPixel($emu = 1)
227229
return round($emu / self::PIXEL_TO_EMU);
228230
}
229231

232+
/**
233+
* Convert an absolute CSS measurement to pixels
234+
*
235+
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
236+
*
237+
* Note that the result will be rounded to the nearest pixel
238+
*
239+
* @param string $cssMeasurement If no measurement unit is included then cm
240+
* is assumed
241+
*
242+
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
243+
*
244+
* @return float
245+
*/
246+
public static function cssToPixel($cssMeasurement = '1cm')
247+
{
248+
$units = trim(preg_replace('/^-?(?:\\d+\\.\\d+|\\.?\\d+)/', '', trim($cssMeasurement)));
249+
$value = preg_replace('/\\D+$/', '', trim($cssMeasurement));
250+
if ((strlen($value) > 0) && ($value[0] == '.')) {
251+
$value = '0' . $value;
252+
}
253+
switch (strtolower($units)) {
254+
case 'in':
255+
$pixel = $value * static::INCH_TO_PIXEL;
256+
break;
257+
case 'cm':
258+
case '':
259+
$pixel = ($value / static::INCH_TO_CM) * static::INCH_TO_PIXEL;
260+
break;
261+
case 'mm':
262+
$pixel = ($value / (10 * static::INCH_TO_CM)) * static::INCH_TO_PIXEL;
263+
break;
264+
case 'pt':
265+
$pixel = ($value / static::INCH_TO_POINT) * static::INCH_TO_PIXEL;
266+
break;
267+
case 'pc':
268+
$pixel = ($value / (12 * static::INCH_TO_POINT)) * static::INCH_TO_PIXEL;
269+
break;
270+
case 'px':
271+
$pixel = floatval($value);
272+
break;
273+
default:
274+
throw new InvalidStyleException($cssMeasurement . ' is an unsupported CSS measurement');
275+
}
276+
return $pixel;
277+
}
278+
279+
/**
280+
* Convert an absolute CSS measurement to EMU
281+
*
282+
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
283+
*
284+
* @param string $cssMeasurement If no measurement unit is included then cm
285+
* is assumed
286+
*
287+
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
288+
*
289+
* @return float
290+
*/
291+
public static function cssToEmu($cssMeasurement = '1cm')
292+
{
293+
return static::cssToPixel($cssMeasurement) * static::PIXEL_TO_EMU;
294+
}
295+
296+
/**
297+
* Convert an absolute CSS measurement to point
298+
*
299+
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
300+
*
301+
* @param string $cssMeasurement If no measurement unit is included then cm
302+
* is assumed
303+
*
304+
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
305+
*
306+
* @return float
307+
*/
308+
public static function cssToPoint($cssMeasurement = '1cm')
309+
{
310+
return static::cssToPixel($cssMeasurement) * static::INCH_TO_POINT / static::INCH_TO_PIXEL;
311+
}
312+
313+
/**
314+
* Convert an absolute CSS measurement to twip
315+
*
316+
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
317+
*
318+
* @param string $cssMeasurement If no measurement unit is included then cm
319+
* is assumed
320+
*
321+
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
322+
*
323+
* @return float
324+
*/
325+
public static function cssToTwip($cssMeasurement = '1cm')
326+
{
327+
return static::cssToPixel($cssMeasurement) * static::INCH_TO_TWIP / static::INCH_TO_PIXEL;
328+
}
329+
330+
/**
331+
* Convert an absolute CSS measurement to centimeter
332+
*
333+
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
334+
*
335+
* @param string $cssMeasurement If no measurement unit is included then cm
336+
* is assumed
337+
*
338+
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
339+
*
340+
* @return float
341+
*/
342+
public static function cssToCm($cssMeasurement = '1cm')
343+
{
344+
return static::cssToPixel($cssMeasurement) * static::INCH_TO_CM / static::INCH_TO_PIXEL;
345+
}
346+
230347
/**
231348
* Convert degree to angle
232349
*

0 commit comments

Comments
 (0)