/** * Helper function that returns scaled and croped images for media output * * Used by image components in getMediaOutput. * Tries to avoid scaling if not required (to keep gif animation intact) */ public static function getMediaOutputForDimension($data, $dim, $type) { // calculate output width/height on base of getImageDimensions and given width $width = substr($type, strlen(Kwf_Media::DONT_HASH_TYPE_PREFIX)); $width = substr($width, 0, strpos($width, '-')); if ($width) { $width = Kwf_Media_Image::getResponsiveWidthStep($width, Kwf_Media_Image::getResponsiveWidthSteps($dim, isset($data['image']) ? $data['image'] : $data['file'])); $dim['height'] = $width / $dim['width'] * $dim['height']; $dim['width'] = $width; } $ret = array(); if (isset($data['image'])) { $output = Kwf_Media_Image::scale($data['image'], $dim); $ret['contents'] = $output; } else { $sourceSize = @getimagesize($data['file']); $scalingNeeded = true; $resultingSize = Kwf_Media_Image::calculateScaleDimensions($data['file'], $dim); if ($sourceSize && array($resultingSize['crop']['width'], $resultingSize['crop']['height']) == array($sourceSize[0], $sourceSize[1]) && array($resultingSize['width'], $resultingSize['height']) == array($sourceSize[0], $sourceSize[1])) { $scalingNeeded = false; } if ($scalingNeeded) { //NOTE: don't pass actual size of the resulting image, scale() will calculate that on it's own //else size is calculated twice and we get rounding errors $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null; $output = Kwf_Media_Image::scale($data['file'], $dim, $uploadId); $ret['contents'] = $output; } else { $ret['file'] = $data['file']; } } $ret['mimeType'] = $data['mimeType']; $ret['mtime'] = filemtime($data['file']); return $ret; }
/** * Helper function that returns scaled and croped images for media output * * Used by image components in getMediaOutput. * Tries to avoid scaling if not required (to keep gif animation intact) */ public static function getMediaOutputForDimension($data, $dim, $type) { if (isset($data['url'])) { $file = Kwf_Config::getValue('mediaCacheDir') . '/remotefile_' . md5($data['url']); if (!is_file($file)) { $httpClientConfig = array('timeout' => 20, 'persistent' => false); if (Kwf_Config::getValue('http.proxy.host')) { $httpClientConfig['adapter'] = 'Zend_Http_Client_Adapter_Proxy'; $httpClientConfig['proxy_host'] = Kwf_Config::getValue('http.proxy.host'); $httpClientConfig['proxy_port'] = Kwf_Config::getValue('http.proxy.port'); } $httpClient = new Zend_Http_Client($data['url'], $httpClientConfig); $request = $httpClient->request(); if ($request->getStatus() == 200) { file_put_contents($file, $request->getBody()); if (!getimagesize($file)) { unlink($file); throw new Kwf_Exception('File is no image: ' . $data['url']); } } else { throw new Kwf_Exception('Could not download file: ' . $data['url']); } } $data['file'] = $file; } // calculate output width/height on base of getImageDimensions and given width $width = substr($type, strlen(Kwf_Media::DONT_HASH_TYPE_PREFIX)); $width = substr($width, 0, strpos($width, '-')); if ($width) { $width = Kwf_Media_Image::getResponsiveWidthStep($width, Kwf_Media_Image::getResponsiveWidthSteps($dim, isset($data['image']) ? $data['image'] : $data['file'])); $dim['height'] = $width / $dim['width'] * $dim['height']; $dim['width'] = $width; } $ret = array(); if (isset($data['image'])) { $output = Kwf_Media_Image::scale($data['image'], $dim); $ret['contents'] = $output; } else { $sourceSize = @getimagesize($data['file']); $scalingNeeded = true; $resultingSize = Kwf_Media_Image::calculateScaleDimensions($data['file'], $dim); if ($sourceSize && array($resultingSize['crop']['width'], $resultingSize['crop']['height']) == array($sourceSize[0], $sourceSize[1]) && array($resultingSize['width'], $resultingSize['height']) == array($sourceSize[0], $sourceSize[1])) { $scalingNeeded = false; } if ($scalingNeeded) { //NOTE: don't pass actual size of the resulting image, scale() will calculate that on it's own //else size is calculated twice and we get rounding errors $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null; $output = Kwf_Media_Image::scale($data['file'], $dim, $uploadId); $ret['contents'] = $output; } else { $ret['file'] = $data['file']; } } $ret['mimeType'] = $data['mimeType']; $ret['mtime'] = filemtime($data['file']); return $ret; }
/** * 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; }
/** * 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 testGetResponsiveWidthStep() { $steps = array(10, 50, 100, 500); $step = Kwf_Media_Image::getResponsiveWidthStep(1, $steps); $this->assertEquals($step, 10); $step = Kwf_Media_Image::getResponsiveWidthStep(9, $steps); $this->assertEquals($step, 10); $step = Kwf_Media_Image::getResponsiveWidthStep(10, $steps); $this->assertEquals($step, 10); $step = Kwf_Media_Image::getResponsiveWidthStep(11, $steps); $this->assertEquals($step, 50); $step = Kwf_Media_Image::getResponsiveWidthStep(99, $steps); $this->assertEquals($step, 100); $step = Kwf_Media_Image::getResponsiveWidthStep(100, $steps); $this->assertEquals($step, 100); $step = Kwf_Media_Image::getResponsiveWidthStep(700, $steps); $this->assertEquals($step, 500); $step = Kwf_Media_Image::getResponsiveWidthStep(500, $steps); $this->assertEquals($step, 500); }
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; }