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        if(!preg_match('/^(\d+?(\.\d*)?)(in|em|ex|px|pt|pc|cm|mm)?$/', $value, $m)) return 0;
28
29        $digit = (double) $m[1];
30        $unit  = (string) $m[3];
31
32        $dpi         = 72;
33        $conversions = array(
34            'in' => $dpi,
35            'em' => 16,
36            'ex' => 12,
37            'px' => 1,
38            'pt' => $dpi / 72, # 1/27 of an inch
39            'pc' => $dpi / 6, # 1/6 of an inch
40            'cm' => $dpi / 2.54, # inch to cm
41            'mm' => $dpi / (2.54 * 10), # inch to cm,
42        );
43
44        if(isset($conversions[$unit])) {
45            $digit = $digit * (float) $conversions[$unit];
46        }
47
48        return $digit;
49    }
50
51
52}
53