Source of file ImageModel.php
Size: 2,942 Bytes - Last Modified: 2021-12-23T10:52:54+00:00
/var/www/docs.ssmods.com/process/src/src/Model/ImageModel.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | <?php namespace Toast\ShopAPI\Model; use SilverStripe\Assets\Image; use SilverStripe\Assets\Image_Backend; use SilverStripe\Core\Config\Config; /** * Class ImageModel */ class ImageModel extends ShopModelBase { /** @var Image $image */ protected $image; protected $image_id; protected $alt; protected $orientation; protected $sizes = []; protected static $fields = [ 'alt', 'sizes', 'orientation' ]; public function __construct($id) { parent::__construct(); if ($id && is_numeric($id)) { // Get the image object $image = Image::get_by_id(Image::class, $id); if ($image && $image->exists()) { $this->extend('updateImage', $image); $this->image = $image; switch ($this->image->getOrientation()) { case Image_Backend::ORIENTATION_LANDSCAPE: $this->orientation = 'wider'; break; case Image_Backend::ORIENTATION_PORTRAIT: $this->orientation = 'taller'; break; default: $this->orientation = 'square'; break; } // Set the initial properties $this->image_id = $this->image->ID; $this->alt = $this->image->Title; } } else { $this->image_id = 0; $this->alt = 'Placeholder'; $this->image = $this->extend('updateImagePlaceholder'); } $this->sizes = $this->getImageSizes(); } /** * Based on config, return an array of image sizes * * @return array */ public function getImageSizes() { $sizes = Config::inst()->get(ImageModel::class, 'image_sizes'); $cropType = Config::inst()->get(ImageModel::class, 'crop_type') ?: 'FitMax'; $images = []; if (is_array($sizes)) { foreach ($sizes as $size => $d) { if (isset($d['width']) && isset($d['height'])) { if ($this->image && $this->image->exists()) { $images[$size] = [ 'src' => $this->image->{$cropType}($d['width'], $d['height'])->getAbsoluteURL(), 'width' => $d['width'], 'height' => $d['height'] ]; } else { $images[$size] = [ 'src' => sprintf('http://placehold.it/%sx%s', $d['width'], $d['height']), 'width' => $d['width'], 'height' => $d['height'] ]; } } } } return $images; } } |