Convert sizes like 2em, 10cm or 12pt to pixels
public static getPixelValue ( string $size ) : integer | ||
$size | string | The size string |
return | integer | The pixel value |
/** * Tests the getPixelValue() method. * * @param string $value * @param int $expected * * @dataProvider getGetPixelValueData */ public function testGetPixelValue($value, $expected) { $this->assertSame($expected, Image::getPixelValue($value)); }
/** * Return an object property * * @param string $strKey The property name * * @return mixed The property value */ public function __get($strKey) { switch ($strKey) { case 'size': case 'filesize': return filesize(TL_ROOT . '/' . $this->strFile); break; case 'name': case 'basename': if (!isset($this->arrPathinfo[$strKey])) { $this->arrPathinfo = $this->getPathinfo(); } return $this->arrPathinfo['basename']; break; case 'dirname': case 'filename': if (!isset($this->arrPathinfo[$strKey])) { $this->arrPathinfo = $this->getPathinfo(); } return $this->arrPathinfo[$strKey]; case 'extension': if (!isset($this->arrPathinfo['extension'])) { $this->arrPathinfo = $this->getPathinfo(); } return strtolower($this->arrPathinfo['extension']); break; case 'origext': if (!isset($this->arrPathinfo['extension'])) { $this->arrPathinfo = $this->getPathinfo(); } return $this->arrPathinfo['extension']; break; case 'tmpname': return basename($this->strTmp); break; case 'path': case 'value': return $this->strFile; break; case 'mime': return $this->getMimeType(); break; case 'hash': return $this->getHash(); break; case 'ctime': return filectime(TL_ROOT . '/' . $this->strFile); break; case 'mtime': return filemtime(TL_ROOT . '/' . $this->strFile); break; case 'atime': return fileatime(TL_ROOT . '/' . $this->strFile); break; case 'icon': return $this->getIcon(); break; case 'dataUri': return 'data:' . $this->mime . ';base64,' . base64_encode($this->getContent()); break; case 'imageSize': if (empty($this->arrImageSize)) { if ($this->isGdImage) { $this->arrImageSize = @getimagesize(TL_ROOT . '/' . $this->strFile); } elseif ($this->isSvgImage) { $doc = new \DOMDocument(); if ($this->extension == 'svgz') { $doc->loadXML(gzdecode($this->getContent())); } else { $doc->loadXML($this->getContent()); } $svgElement = $doc->documentElement; if ($svgElement->getAttribute('width') && $svgElement->getAttribute('height') && substr(rtrim($svgElement->getAttribute('width')), -1) != '%' && substr(rtrim($svgElement->getAttribute('height')), -1) != '%') { $this->arrImageSize = array(\Image::getPixelValue($svgElement->getAttribute('width')), \Image::getPixelValue($svgElement->getAttribute('height'))); } if ($this->arrImageSize && $this->arrImageSize[0] && $this->arrImageSize[1]) { $this->arrImageSize[2] = 0; // replace this with IMAGETYPE_SVG when it becomes available $this->arrImageSize[3] = 'width="' . $this->arrImageSize[0] . '" height="' . $this->arrImageSize[1] . '"'; $this->arrImageSize['bits'] = 8; $this->arrImageSize['channels'] = 3; $this->arrImageSize['mime'] = $this->getMimeType(); } else { $this->arrImageSize = false; } } } return $this->arrImageSize; break; case 'width': return $this->imageSize[0]; break; case 'height': return $this->imageSize[1]; break; case 'imageViewSize': if (empty($this->arrImageViewSize)) { if ($this->imageSize) { $this->arrImageViewSize = array($this->imageSize[0], $this->imageSize[1]); } elseif ($this->isSvgImage) { $doc = new \DOMDocument(); if ($this->extension == 'svgz') { $doc->loadXML(gzdecode($this->getContent())); } else { $doc->loadXML($this->getContent()); } $svgElement = $doc->documentElement; if ($svgElement->getAttribute('viewBox')) { $svgViewBox = preg_split('/[\\s,]+/', $svgElement->getAttribute('viewBox')); $this->arrImageViewSize = array(intval($svgViewBox[2]), intval($svgViewBox[3])); } if (!$this->arrImageViewSize || !$this->arrImageViewSize[0] || !$this->arrImageViewSize[1]) { $this->arrImageViewSize = false; } } } return $this->arrImageViewSize; break; case 'viewWidth': return $this->imageViewSize[0]; break; case 'viewHeight': return $this->imageViewSize[1]; break; case 'isImage': return $this->isGdImage || $this->isSvgImage; break; case 'isGdImage': return in_array($this->extension, array('gif', 'jpg', 'jpeg', 'png')); break; case 'isSvgImage': return in_array($this->extension, array('svg', 'svgz')); break; case 'channels': return $this->imageSize['channels']; break; case 'bits': return $this->imageSize['bits']; break; case 'isRgbImage': return $this->channels == 3; break; case 'isCmykImage': return $this->channels == 4; break; case 'handle': if (!is_resource($this->resFile)) { $this->resFile = fopen(TL_ROOT . '/' . $this->strFile, 'rb'); } return $this->resFile; break; default: return parent::__get($strKey); break; } }