Skip to content

Commit

Permalink
Merge branch 'insert-image' of https://github.com/faegi/PHPWord into …
Browse files Browse the repository at this point in the history
…develop
  • Loading branch information
mark-gerarts committed May 29, 2018
2 parents e02c5f7 + 0e210b4 commit b6dcd15
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 5 deletions.
93 changes: 91 additions & 2 deletions src/PhpWord/Shared/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Shared;

use PhpOffice\PhpWord\Exception\InvalidStyleException;

/**
* Common converter functions
*/
Expand Down Expand Up @@ -239,6 +241,87 @@ public static function picaToPoint($pica = 1)
return $pica / self::INCH_TO_PICA * self::INCH_TO_POINT;
}

/**
* Convert an absolute CSS measurement to pixels
*
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
*
* Note that the result will be rounded to the nearest pixel
*
* @param string $cssMeasurement If no measurement unit is included then cm
* is assumed
*
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
*
* @return float
*/
public static function cssToPixel($cssMeasurement = '1cm')
{
$units = trim(preg_replace('/^-?(?:\\d+\\.\\d+|\\.?\\d+)/', '', trim($cssMeasurement)));
$value = preg_replace('/\\D+$/', '', trim($cssMeasurement));
if ((strlen($value) > 0) && ($value[0] == '.')) {
$value = '0' . $value;
}
switch (strtolower($units)) {
case 'in':
$pixel = $value * static::INCH_TO_PIXEL;
break;
case 'cm':
case '':
$pixel = ($value / static::INCH_TO_CM) * static::INCH_TO_PIXEL;
break;
case 'mm':
$pixel = ($value / (10 * static::INCH_TO_CM)) * static::INCH_TO_PIXEL;
break;
case 'pt':
$pixel = ($value / static::INCH_TO_POINT) * static::INCH_TO_PIXEL;
break;
case 'pc':
$pixel = ($value / (12 * static::INCH_TO_POINT)) * static::INCH_TO_PIXEL;
break;
case 'px':
$pixel = floatval($value);
break;
default:
throw new InvalidStyleException($cssMeasurement . ' is an unsupported CSS measurement');
}
return $pixel;
}

/**
* Convert an absolute CSS measurement to EMU
*
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
*
* @param string $cssMeasurement If no measurement unit is included then cm
* is assumed
*
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
*
* @return float
*/
public static function cssToEmu($cssMeasurement = '1cm')
{
return static::cssToPixel($cssMeasurement) * static::PIXEL_TO_EMU;
}

/**
* Convert an absolute CSS measurement to centimeter
*
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
*
* @param string $cssMeasurement If no measurement unit is included then cm
* is assumed
*
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
*
* @return float
*/
public static function cssToCm($cssMeasurement = '1cm')
{
return static::cssToPixel($cssMeasurement) * static::INCH_TO_CM / static::INCH_TO_PIXEL;
}

/**
* Convert degree to angle
*
Expand Down Expand Up @@ -323,9 +406,15 @@ public static function cssToPoint($value)
}

/**
* Transforms a size in CSS format (eg. 10px, 10px, ...) to twips
* Convert an absolute CSS measurement to twip
*
* Units for absolute CSS measurements are cm, mm, in, px, pt and pc
*
* @param string $cssMeasurement If no measurement unit is included then cm
* is assumed
*
* @throws \PHPOffice\PhpWord\Exception\InvalidStyleException
*
* @param string $value
* @return float
*/
public static function cssToTwip($value)
Expand Down
Loading

0 comments on commit b6dcd15

Please sign in to comment.