1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16class Unit
17{
18
19    /**
20     * Calculates pixel size for any given SVG size
21     *
22     * @credit https://github.com/lechup/svgpureinsert/blob/master/helper.php#L72
23     * @param $value
24     * @return int
25     */
26    static public function toPixel($value)
27    {
28        if (!preg_match('/^(\d+?(\.\d*)?)(in|em|ex|px|pt|pc|cm|mm)?$/', $value, $m)) return 0;
29
30        $digit = (double)$m[1];
31        $unit = $m[3] ?? null;
32
33        $dpi = 72;
34        $conversions = array(
35            'in' => $dpi,
36            'em' => 16,
37            'ex' => 12,
38            'px' => 1,
39            'pt' => $dpi / 72, # 1/27 of an inch
40            'pc' => $dpi / 6, # 1/6 of an inch
41            'cm' => $dpi / 2.54, # inch to cm
42            'mm' => $dpi / (2.54 * 10), # inch to cm,
43        );
44
45        if (isset($conversions[$unit])) {
46            $digit = $digit * (float)$conversions[$unit];
47        }
48
49        return $digit;
50    }
51
52
53}
54