public function getTemplateVars() { $ret = parent::getTemplateVars(); $ret['image'] = $this->getData(); $imageCaptionSetting = Kwc_Abstract::getSetting($this->_getSetting('masterComponentClass'), 'imageCaption'); if ($imageCaptionSetting) { $ret['image_caption'] = $this->getRow()->image_caption; } $ret['baseUrl'] = $this->getBaseImageUrl(); if ($this->getRow()->own_image) { $imageData = $this->getImageData(); if ($imageData) { $steps = Kwf_Media_Image::getResponsiveWidthSteps($this->getImageDimensions(), $imageData['file']); $ret['minWidth'] = $steps[0]; $ret['maxWidth'] = end($steps); } } if (Kwc_Abstract::getSetting($this->_getSetting('masterComponentClass'), 'altText')) { $ret['altText'] = $this->_getRow()->alt_text; } if (Kwc_Abstract::getSetting($this->_getSetting('masterComponentClass'), 'titleText')) { $ret['imgAttributes']['title'] = $this->_getRow()->title_text; } return $ret; }
protected function _fireMediaChanged(Kwf_Component_Data $c) { $imageData = $c->getComponent()->getImageDataOrEmptyImageData(); $this->fireEvent(new Kwc_Abstract_Image_ImageChangedEvent($this->_class, $c)); if ($imageData) { $types = array(); $typeBase = $c->getComponent()->getBaseType(); // Kwc_Abstract_Image_Component->getBaseImageUrl is cached in Kwf_Media and uses therefore the base type $types[] = $typeBase; $dim = $c->getComponent()->getImageDimensions(); if (isset($imageData['dimensions'])) { $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['dimensions']); } else { if (isset($imageData['file'])) { $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); } else { throw new Kwf_Exception("Can't detect responsiveWidthSteps as image dimensions are unknown"); } } foreach ($steps as $step) { $types[] = str_replace('{width}', $step, $typeBase); } $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $c, $types)); } $this->fireEvent(new Kwf_Component_Event_Component_ContentWidthChanged($this->_class, $c)); $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c)); }
/** * This function is used by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Component * to get the url to show the image from parent with dimension defined through * this component. */ public function getImageUrl() { $baseUrl = $this->getBaseImageUrl(); if ($baseUrl) { $dimensions = $this->getImageDimensions(); $imageData = $this->getImageData(); $width = Kwf_Media_Image::getResponsiveWidthStep($dimensions['width'], Kwf_Media_Image::getResponsiveWidthSteps($dimensions, $imageData['file'])); return str_replace('{width}', $width, $baseUrl); } return null; }
protected function _fireMediaChanged(Kwf_Component_Data $c) { $imageData = $c->getComponent()->getImageDataOrEmptyImageData(); $this->fireEvent(new Kwc_Abstract_Image_ImageChangedEvent($this->_class, $c)); if ($imageData) { $typeBase = $c->getComponent()->getBaseType(); // Kwc_Abstract_Image_Component->getBaseImageUrl is cached in Kwf_Media and uses therefore the base type $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $c, $typeBase)); $dim = $c->getComponent()->getImageDimensions(); $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); foreach ($steps as $step) { $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $c, str_replace('{width}', $step, $typeBase))); } } $this->fireEvent(new Kwf_Component_Event_Component_ContentWidthChanged($this->_class, $c)); $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c)); }
public function onImageChanged(Kwc_Abstract_Image_ImageChangedEvent $event) { $components = $event->component->getRecursiveChildComponents(array('componentClass' => $this->_class, 'ignoreVisible' => true)); //ignore visible because we need to clear media cache for invisible images too (as it's shown in preview) foreach ($components as $component) { $imageData = $component->getComponent()->getImageData(); if ($imageData) { $dim = $component->getComponent()->getImageDimensions(); $typeBase = $component->getComponent()->getBaseType(); $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); foreach ($steps as $step) { $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $component, str_replace('{width}', $step, $typeBase))); } } $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $component)); } }
protected function _clearMediaCache($components) { if (!is_array($components)) { $components = array($components); } foreach ($components as $component) { $imageData = $component->getComponent()->getImageData(); if ($imageData) { $dim = $component->getComponent()->getImageDimensions(); $typeBase = $component->getComponent()->getBaseType(); $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); foreach ($steps as $step) { $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $component, str_replace('{width}', $step, $typeBase))); } } } }
public function onMasterImageChanged(Kwc_Abstract_Image_ImageChangedEvent $event) { $chained = Kwc_Chained_Abstract_Component::getAllChainedByMaster($event->component, 'Trl'); foreach ($chained as $c) { $components = $c->getRecursiveChildComponents(array('componentClass' => $this->_class)); foreach ($components as $component) { $imageData = $component->getComponent()->getImageData(); $dim = $component->getComponent()->getImageDimensions(); $typeBase = $component->getComponent()->getBaseType(); $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); foreach ($steps as $step) { $this->fireEvent(new Kwf_Events_Event_Media_Changed($this->_class, $component, str_replace('{width}', $step, $typeBase))); } $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $component)); } } }
/** * This function is used by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Component * to get the url to show the image from parent with dimension defined through * this component. */ public function getImageUrl() { $baseUrl = $this->getBaseImageUrl(); if ($baseUrl) { $dimensions = $this->getImageDimensions(); $imageData = $this->getImageData(); $width = Kwf_Media_Image::getResponsiveWidthStep($dimensions['width'], Kwf_Media_Image::getResponsiveWidthSteps($dimensions, $imageData['file'])); $ret = str_replace('{width}', $width, $baseUrl); $ev = new Kwf_Component_Event_CreateMediaUrl($this->getData()->componentClass, $this->getData(), $ret); Kwf_Events_Dispatcher::fireEvent($ev); return $ev->url; } return null; }
public function getImageUrl() { $data = $this->_getImageDataOrEmptyImageData(); if ($data) { $s = $this->getImageDimensions(); $imageData = $this->_getImageDataOrEmptyImageData(); $width = Kwf_Media_Image::getResponsiveWidthStep($s['width'], Kwf_Media_Image::getResponsiveWidthSteps($s, $imageData['dimensions'])); return $this->_getImageUrl($width); } return null; }
public function testOldMediaUrlImage() { $c = $this->_root->getComponentById(1015)->getComponent(); $dim = $this->_root->getComponentById(1015)->getChildComponent('-i1')->getComponent()->getImageDimensions(); $imageData = $this->_root->getComponentById(1015)->getChildComponent('-i1')->getComponent()->getImageData(); $width = Kwf_Media_Image::getResponsiveWidthStep($dim['width'], Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file'])); $row = $c->getRow(); $html = '<p><img src="/media/Kwc_Basic_Text_Image_TestComponent/1015-i1/File/small/e73520d11dee6ff49859b8bb26fc631f/filename.jpg?319" /></p>'; $row->content = $html; $row->save(); $html = $row->content; $this->assertEquals("<p>\n <img src=\n \"/media/Kwc_Basic_Text_Image_TestComponent/1015-i1/File/small/e73520d11dee6ff49859b8bb26fc631f/filename.jpg?319\" />\n</p>", $html); $html = $c->getData()->render(); $this->assertRegExp('#^\\s*<div class="webStandard kwcText kwcBasicTextTestComponent">' . '\\s*<p>\\s*<div class="kwcAbstractComposite kwcAbstractImage kwcBasicTextImageTestComponent[^"]*".*>' . '\\s*<div class="container" .*>' . '\\s*<noscript>' . '\\s*<img src="/kwf/kwctest/Kwc_Basic_Text_Root/media/Kwc_Basic_Text_Image_TestComponent/1015-i1/dh-' . $width . '-[0-9a-z]+/[^/]+/[0-9]+/foo.png" width="100" height="100" alt="" />' . '\\s*</noscript>#ms', $html); }
public function testGetResponsiveWidthStepsImageBiggerThanDpr2() { $imageData['file'] = '../images/stripesDark.png'; $dim['width'] = 15; $dim['height'] = 100; $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); $resultingSteps = array(15, 30); $this->assertEquals($steps, $resultingSteps); $imageData['file'] = '../images/avatar_ghost.jpg'; $dim['width'] = 70; $dim['height'] = 100; $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); $resultingSteps = array(70, 140); $this->assertEquals($steps, $resultingSteps); $imageData['file'] = '../images/devices/macBook.jpg'; $dim['width'] = 934; $dim['height'] = 100; $steps = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['file']); $resultingSteps = array(34, 134, 234, 334, 434, 534, 634, 734, 834, 934, 1034, 1134, 1234, 1334, 1434, 1534, 1634, 1734, 1834, 1868); $this->assertEquals($steps, $resultingSteps); }
public function getImageUrl() { $data = $this->_getImageDataOrEmptyImageData(); if ($data) { $s = $this->getImageDimensions(); $imageData = $this->_getImageDataOrEmptyImageData(); $width = Kwf_Media_Image::getResponsiveWidthStep($s['width'], Kwf_Media_Image::getResponsiveWidthSteps($s, $imageData['file'])); if (Kwc_Abstract::getSetting($this->getData()->componentClass, 'useDataUrl')) { $id = $this->getData()->componentId; $type = str_replace('{width}', $width, $this->getBaseType()); $data = self::getMediaOutput($id, $type, $this->getData()->componentClass); if (isset($data['file'])) { $c = file_get_contents($data['file']); } else { $c = $data['contents']; } $base64 = base64_encode($c); if (strlen($base64) < 32 * 1024) { $mime = $data['mimeType']; return "data:{$mime};base64,{$base64}"; } } return str_replace('{width}', $width, $this->getBaseImageUrl()); } return null; }
/** * Checks if given type (starting with Kwf_Media::DONT_HASH_TYPE_PREFIX) should * return an image by checking Kwf_Media_Image::getResponsiveWidthSteps of image */ public static function isValidImage($id, $type, $className) { $isValid = Kwf_Media_Output_Component::isValid($id); if ($isValid == Kwf_Media_Output_IsValidInterface::VALID || $isValid == Kwf_Media_Output_IsValidInterface::VALID_DONT_CACHE) { // Can be searched with ignore-visible because if it is invisble and // not allowed to show Kwf_Media_Output_Component::isValid would return // invalid or access_denied $c = Kwf_Component_Data_Root::getInstance()->getComponentById($id, array('ignoreVisible' => true)); if (!$c) { return Kwf_Media_Output_IsValidInterface::INVALID; } if ($c->componentClass != $className) { return Kwf_Media_Output_IsValidInterface::INVALID; } $baseType = $c->getComponent()->getBaseType(); $dim = $c->getComponent()->getImageDimensions(); $imageData = $c->getComponent()->getImageDataOrEmptyImageData(); $widths = Kwf_Media_Image::getResponsiveWidthSteps($dim, $imageData['dimensions']); $ok = false; foreach ($widths as $w) { if (str_replace('{width}', $w, $baseType) == $type) { $ok = true; break; } } if (!$ok) { $isValid = Kwf_Media_Output_IsValidInterface::INVALID; } } return $isValid; }