Source of file PlaceholderImagePageExtension.php
Size: 2,438 Bytes - Last Modified: 2021-12-23T10:38:24+00:00
/var/www/docs.ssmods.com/process/src/code/PlaceholderImagePageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | <?php /** * Description of ImagePageExtension. * * @author Tom Densham <tom.densham@studiobonito.co.uk> * @copyright (c) 2012, Studio Bonito Ltd. * @version 1.0 */ class PlaceholderImagePageExtension extends DataExtension { /** * Return a place holder image of a certain size. * * @param int $width * @param int $height * @return Image | string */ public function PlaceholderImage($width = null, $height = null) { $image = $this->getPlaceholderImageRecursive($this->owner); if(!isset($image)) { $config = SiteConfig::current_site_config(); $image = $config->PlaceholderImage(); } if(isset($image) && $image->exists()) { if($image->hasMethod('RatioCrop') && ($width) && ($height)) { return $image->RatioCrop($width, $height); } return $image; } $width = $width ?: 690; $height = $height ?: 230; return "<img src=\"http://placehold.it/{$width}x{$height}\" width=\"{$width}\" height=\"{$height}\" alt=\"\" />"; } public function PlaceholderImageURL($width = null, $height = null) { $image = $this->getPlaceholderImageRecursive($this->owner); if(!isset($image)) { $config = SiteConfig::current_site_config(); $image = $config->PlaceholderImage(); } if(isset($image) && $image->exists()) { if($image->hasMethod('RatioCrop') && ($width) && ($height)) { return $image->RatioCrop($width, $height).URL; } return $image.URL; } $width = $width ?: 690; $height = $height ?: 230; return "http://placehold.it/{$width}x{$height}"; } /** * Recursivly search for a PlaceholderImage. * * @param DataObject $object * @return Image | null */ protected function getPlaceholderImageRecursive(DataObject $object) { if($object->has_one('PlaceholderImage')) { $image = $object->getComponent('PlaceholderImage'); if($image->exists()) { return $image; } } $parentObject = $object->hasMethod('Parent') ? $object->Parent() : null; return isset($parentObject) && $parentObject->exists() ? $this->getPlaceholderImageRecursive($parentObject) : null; } } |