Contao\Image::getPixelValue PHP Méthode

getPixelValue() public static méthode

Convert sizes like 2em, 10cm or 12pt to pixels
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static getPixelValue ( string $size ) : integer
$size string The size string
Résultat integer The pixel value
    public static function getPixelValue($size)
    {
        @trigger_error('Using Image::getPixelValue() has been deprecated and will no longer work in Contao 5.0.', E_USER_DEPRECATED);
        $value = preg_replace('/[^0-9.-]+/', '', $size);
        $unit = preg_replace('/[^acehimnprtvwx%]/', '', $size);
        // Convert 16px = 1em = 2ex = 12pt = 1pc = 1/6in = 2.54/6cm = 25.4/6mm = 100%
        switch ($unit) {
            case '':
            case 'px':
                return (int) round($value);
                break;
            case 'em':
                return (int) round($value * 16);
                break;
            case 'ex':
                return (int) round($value * 16 / 2);
                break;
            case 'pt':
                return (int) round($value * 16 / 12);
                break;
            case 'pc':
                return (int) round($value * 16);
                break;
            case 'in':
                return (int) round($value * 16 * 6);
                break;
            case 'cm':
                return (int) round($value * 16 / (2.54 / 6));
                break;
            case 'mm':
                return (int) round($value * 16 / (25.4 / 6));
                break;
            case '%':
                return (int) round($value * 16 / 100);
                break;
        }
        return 0;
    }

Usage Example

 /**
  * Tests the getPixelValue() method.
  *
  * @param string $value
  * @param int    $expected
  *
  * @dataProvider getGetPixelValueData
  */
 public function testGetPixelValue($value, $expected)
 {
     $this->assertSame($expected, Image::getPixelValue($value));
 }
All Usage Examples Of Contao\Image::getPixelValue