Skip to content

Instantly share code, notes, and snippets.

@severak
Last active March 14, 2017 10:04
Show Gist options
  • Save severak/7f3fc90fcd07854c186c9ecfd0d5f4ae to your computer and use it in GitHub Desktop.
Save severak/7f3fc90fcd07854c186c9ecfd0d5f4ae to your computer and use it in GitHub Desktop.
code which determines object size in mPDF
<?php
// mpdf sizes code
//
// extracted from mpdf itsel
//Scale factor
define('_MPDFK', (72 / 25.4));
class mpdf {
// this is only stub class, its not usable
function ConvertSize($size = 5, $maxsize = 0, $fontsize = false, $usefontsize = true)
{
// usefontsize - set false for e.g. margins - will ignore fontsize for % values
// Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
// For text $maxsize = Fontsize
// Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
// Returns values using 'mm' units
$size = trim(strtolower($size));
if ($size == 'thin')
$size = 1 * (25.4 / $this->dpi); //1 pixel width for table borders
elseif (stristr($size, 'px'))
$size *= (25.4 / $this->dpi); //pixels
elseif (stristr($size, 'cm'))
$size *= 10; //centimeters
elseif (stristr($size, 'mm'))
$size += 0; //millimeters
elseif (stristr($size, 'pt'))
$size *= 25.4 / 72; //72 pts/inch
elseif (stristr($size, 'rem')) {
$size += 0; //make "0.83rem" become simply "0.83"
$size *= ($this->default_font_size / _MPDFK);
} elseif (stristr($size, 'em')) {
$size += 0; //make "0.83em" become simply "0.83"
if ($fontsize) {
$size *= $fontsize;
} else {
$size *= $maxsize;
}
} elseif (stristr($size, '%')) {
$size += 0; //make "90%" become simply "90"
if ($fontsize && $usefontsize) {
$size *= $fontsize / 100;
} else {
$size *= $maxsize / 100;
}
} elseif (stristr($size, 'in'))
$size *= 25.4; //inches
elseif (stristr($size, 'pc'))
$size *= 38.1 / 9; //PostScript picas
elseif (stristr($size, 'ex')) { // Approximates "ex" as half of font height
$size += 0; //make "3.5ex" become simply "3.5"
if ($fontsize) {
$size *= $fontsize / 2;
} else {
$size *= $maxsize / 2;
}
} elseif ($size == 'medium')
$size = 3 * (25.4 / $this->dpi); //3 pixel width for table borders
elseif ($size == 'thick')
$size = 5 * (25.4 / $this->dpi); //5 pixel width for table borders
elseif ($size == 'xx-small') {
if ($fontsize) {
$size *= $fontsize * 0.7;
} else {
$size *= $maxsize * 0.7;
}
} elseif ($size == 'x-small') {
if ($fontsize) {
$size *= $fontsize * 0.77;
} else {
$size *= $maxsize * 0.77;
}
} elseif ($size == 'small') {
if ($fontsize) {
$size *= $fontsize * 0.86;
} else {
$size *= $maxsize * 0.86;
}
} elseif ($size == 'medium') {
if ($fontsize) {
$size *= $fontsize;
} else {
$size *= $maxsize;
}
} elseif ($size == 'large') {
if ($fontsize) {
$size *= $fontsize * 1.2;
} else {
$size *= $maxsize * 1.2;
}
} elseif ($size == 'x-large') {
if ($fontsize) {
$size *= $fontsize * 1.5;
} else {
$size *= $maxsize * 1.5;
}
} elseif ($size == 'xx-large') {
if ($fontsize) {
$size *= $fontsize * 2;
} else {
$size *= $maxsize * 2;
}
} else
$size *= (25.4 / $this->dpi); //nothing == px
return $size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment