Bolt\Twig\Handler\ImageHandler::showImage PHP Метод

showImage() публичный Метод

Set width or height parameter to '0' for proportional scaling. Set them both to null (or not at all) to get the default size from config.yml. Example: {{ content.image|showimage(320, 240) }} Example: {{ showimage(content.image, 320, 240) }}
public showImage ( string $fileName = null, integer $width = null, integer $height = null, string $crop = null ) : string
$fileName string Image filename
$width integer Image width
$height integer Image height
$crop string Crop image string identifier
Результат string HTML output
    public function showImage($fileName = null, $width = null, $height = null, $crop = null)
    {
        if (empty($fileName)) {
            return '';
        }
        $thumb = $this->getThumbnail($fileName, $width, $height, $crop);
        if ($width === null && $height === null) {
            $thumbconf = $this->app['config']->get('general/thumbnails');
            $width = !empty($thumbconf['default_image'][0]) ? $thumbconf['default_image'][0] : 1000;
            $height = !empty($thumbconf['default_image'][1]) ? $thumbconf['default_image'][1] : 750;
            $thumb->setWidth($width);
            $thumb->setHeight($height);
        } elseif ($width === null xor $height === null) {
            $info = $this->imageInfo($thumb->getFileName(), false)->getInfo();
            if ($width !== null) {
                $width = min($width, $info->getWidth());
                $thumb->setHeight(round($width / $info->getAspectRatio()));
            } elseif ($height !== null) {
                $height = min($height, $info->getHeight());
                $thumb->setWidth(round($height * $info->getAspectRatio()));
            } else {
                $thumb->setWidth($info->getWidth());
                $thumb->setHeight($info->getHeight());
            }
        }
        return sprintf('<img src="%s" width="%s" height="%s" alt="%s">', $this->getThumbnailUri($thumb), $thumb->getWidth(), $thumb->getHeight(), $thumb->getAltTitle());
    }

Usage Example

Пример #1
0
 public function testShowImageFileNameArrayWithAlt()
 {
     $app = $this->getApp();
     $handler = new ImageHandler($app);
     $result = $handler->showImage(['title' => 'Koala', 'alt' => 'Gum Leaves', 'filename' => 'generic-logo.png'], null, null, null, null);
     $this->assertSame('<img src="/thumbs/1000x750c/generic-logo.png" width="1000" height="750" alt="Gum Leaves">', $result);
 }