public function prepareSave(Kwf_Model_Row_Interface $row, $postData) { Kwf_Form_Field_Abstract::prepareSave($row, $postData); $value = $this->_getValueFromPostData($postData); if (is_string($value)) { $value = Zend_Json::decode($value); } if (!is_array($value)) { $value = array(); } $row->dimension = isset($value['dimension']) ? $value['dimension'] : null; $row->width = isset($value['width']) && $value['width'] ? $value['width'] : null; $row->height = isset($value['height']) && $value['height'] ? $value['height'] : null; if (isset($value['cropData'])) { $row->crop_x = isset($value['cropData']['x']) && $value['cropData']['x'] !== null ? $value['cropData']['x'] : null; $row->crop_y = isset($value['cropData']['y']) && $value['cropData']['y'] !== null ? $value['cropData']['y'] : null; $row->crop_width = isset($value['cropData']['width']) && $value['cropData']['width'] ? $value['cropData']['width'] : null; $row->crop_height = isset($value['cropData']['height']) && $value['cropData']['height'] ? $value['cropData']['height'] : null; if ($row->getParentRow('Image')) { $scaleFactor = Kwf_Media_Image::getHandyScaleFactor($row->getParentRow('Image')->getFileSource()); if ($scaleFactor != 1) { $row->crop_x = $row->crop_x * $scaleFactor; $row->crop_y = $row->crop_y * $scaleFactor; $row->crop_width = $row->crop_width * $scaleFactor; $row->crop_height = $row->crop_height * $scaleFactor; } } } else { $row->crop_x = null; $row->crop_y = null; $row->crop_width = null; $row->crop_height = null; } }
public function update() { parent::update(); $select = new Kwf_Model_Select(); $select->where(new Kwf_Model_Select_Expr_Or(array(new Kwf_Model_Select_Expr_Equals('dimension', 'customcrop'), new Kwf_Model_Select_Expr_Equals('dimension', 'custombestfit')))); $select->order('dimension', 'asc'); $rows = Kwf_Model_Abstract::getInstance('Kwc_Abstract_Image_Model')->getRows($select); foreach ($rows as $row) { if ($row->dimension == 'customcrop') { $row->dimension = 'custom'; $row->crop_x = null; $row->crop_y = null; $row->crop_width = null; $row->crop_heigth = null; } else { if ($row->dimension == 'custombestfit') { $row->dimension = 'custom'; if ($row->imageExists()) { $targetSize = array('width' => $row->width, 'height' => $row->height, 'cover' => false); $outputSize = Kwf_Media_Image::calculateScaleDimensions($row->getParentRow('Image')->getFileSource(), $targetSize); $row->width = $outputSize['width']; $row->height = $outputSize['height']; $row->crop_x = $outputSize['crop']['x']; $row->crop_y = $outputSize['crop']['y']; $row->crop_width = $outputSize['crop']['width']; $row->crop_height = $outputSize['crop']['height']; } } } $row->save(); } }
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)); }
public function writeContent($setCoordinates = true) { $data = $this->_component->getImageData(); if ($data && $data['file'] && is_file($data['file'])) { $source = $data['file']; $size = $this->getSize(); $imageSize = array('cover' => false, 'width' => $this->_calculateMm($size['width']), 'height' => $this->_calculateMm($size['height'])); $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null; $content = Kwf_Media_Image::scale($source, $imageSize, $uploadId); $filter = new Kwf_Filter_Ascii(); $tempFilename = tempnam('application/temp', 'pdfimage'); file_put_contents($tempFilename, $content); $data = getimagesize($tempFilename); if ($data[2] == 2) { // nur jpgs ausgeben if ($setCoordinates && $this->addPageIfNecessary()) { $x = $this->getX(); $this->SetY($this->getTopMargin()); $this->SetX($x); } $type = str_replace('image/', '', 'image/jpeg'); $this->_pdf->Image($tempFilename, $this->getX(), $this->getY(), $this->_calculatePx($data[0]), $this->_calculatePx($data[1]), $type); if ($setCoordinates) { $this->SetY($this->getY() + $size['height'] + 2); } } unlink($tempFilename); return true; } return false; }
/** * This function is called by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Trl_Component */ public function getImageDimensions() { $dimension = $this->getData()->chained->getComponent()->_getSetting('dimension'); if ($this->getData()->chained->getComponent()->getRow()->use_crop) { $parentDimensions = $this->_getImageEnlargeComponent()->getImageDimensions(); $dimension['crop'] = $parentDimensions['crop']; } $data = $this->getImageData(); return Kwf_Media_Image::calculateScaleDimensions($data['file'], $dimension); }
public static function getMediaOutput($id, $type, $className) { $row = Kwf_Model_Abstract::getInstance($className)->getRow($id); $dim = self::getImageDimensions($type); if ($type == 'LoginImageLarge') { $type = 'LoginImage'; } $uploadRow = $row->getParentRow($type); return array('contents' => Kwf_Media_Image::scale($uploadRow, $dim, $uploadRow->id), 'mimeType' => $row->getParentRow($type)->mime_type); }
/** * 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; }
public static function getDimensionsByRow($row, $type, $fileRow = null) { $model = get_class($row->getModel()); $dim = call_user_func(array($model, 'getImageDimensions'), $type); if (!$fileRow) { $fileRow = $row->getParentRow($type); } if ($fileRow) { return Kwf_Media_Image::calculateScaleDimensions($fileRow->getImageDimensions(), $dim); } return null; }
public static function getMediaOutput($uploadId, $type, $className) { $uploadRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($uploadId); if (preg_match('#^preview-(\\d+)-(\\d+)$#', $type, $m)) { $size = array('width' => $m[1], 'height' => $m[2], 'cover' => false); } else { if ($type == 'hover') { $size = array('width' => 250, 'height' => 250, 'cover' => false); } else { throw new Kwf_Exception_NotFound(); } } return array('contents' => Kwf_Media_Image::scale($uploadRow->getFileSource(), $size, $uploadRow->id), 'mimeType' => $uploadRow->mime_type); }
public static function getMediaOutput($uploadId, $type, $className) { $uploadRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($uploadId); if ($type == 'original') { return array('file' => $uploadRow->getFileSource(), 'mimeType' => $uploadRow->mime_type); } else { if ($type == 'preview') { $size = array('width' => 150, 'height' => 200, 'cover' => false); return array('contents' => Kwf_Media_Image::scale($uploadRow->getFileSource(), $size, $uploadRow->id), 'mimeType' => $uploadRow->mime_type); } else { throw new Kwf_Exception_NotFound(); } } }
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)); } }
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)); } } }
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)); }
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 calculateDimensions() { $db = Kwf_Registry::get('db'); $s = new Kwf_Model_Select(); $it = new Kwf_Model_Iterator_Packages(new Kwf_Model_Iterator_Rows(Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model'), $s)); foreach ($it as $row) { $this->_progressBar->next(1, 'calculating dimension ' . $row->id); if (file_exists($row->getFileSource())) { $size = @getimagesize($row->getFileSource()); if ($size) { $width = $size[0]; $height = $size[1]; $rotation = Kwf_Media_Image::getExifRotation($row->getFileSource()); $db->query("UPDATE `kwf_uploads` SET `is_image`=1, `image_width` = '{$width}', `image_height` = '{$height}', `image_rotation` = '{$rotation}' WHERE `id` = '{$row->id}';"); } } } }
public function testExifRotation() { $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotation0.jpg'); $this->assertEquals($rotation, 0); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotation1.jpg'); $this->assertEquals($rotation, -90); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotation2.jpg'); $this->assertEquals($rotation, 180); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotation3.jpg'); $this->assertEquals($rotation, 90); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotationM0.JPG'); $this->assertEquals($rotation, 0); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotationM1.JPG'); $this->assertEquals($rotation, -90); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotationM2.JPG'); $this->assertEquals($rotation, 180); $rotation = Kwf_Media_Image::getExifRotation('Kwf/Media/ImageExifRotation/rotation/rotationM3.JPG'); $this->assertEquals($rotation, 90); }
public function indexAction() { $this->view->applicationName = Kwf_Config::getValue('application.name'); $this->view->brandingKoala = Kwf_Config::getValue('application.branding.koala'); $this->view->brandingVividPlanet = Kwf_Config::getValue('application.branding.vividPlanet'); $this->view->pages = Kwf_Registry::get('acl')->has('kwf_component_pages'); $this->view->baseUrl = Kwf_Setup::getBaseUrl(); $this->view->favicon = Kwf_View_Ext::getFavicon(); try { $t = new Kwf_Util_Model_Welcome(); $row = $t->getRow(1); } catch (Zend_Db_Statement_Exception $e) { //wenn tabelle nicht existiert fehler abfangen $row = null; } if ($row && ($fileRow = $row->getParentRow('LoginImage'))) { $this->view->image = Kwf_Media::getUrlByRow($row, 'LoginImageLarge', 'login'); $this->view->imageSize = Kwf_Media_Image::calculateScaleDimensions($fileRow->getImageDimensions(), Kwf_Util_Model_Welcome::getImageDimensions('LoginImageLarge')); } else { $this->view->image = false; } if (Kwf_Registry::get('config')->allowUntagged === true) { if (file_exists('.git') && Kwf_Util_Git::web()->getActiveBranch() != 'production') { $this->view->untagged = true; } if (file_exists(KWF_PATH . '/.git') && Kwf_Util_Git::kwf()->getActiveBranch() != 'production/' . Kwf_Registry::get('config')->application->id) { $this->view->untagged = true; } } $this->view->contentScript = $this->getHelper('viewRenderer')->getViewScript('login'); $this->view->lostPasswordLink = $this->getFrontController()->getRouter()->assemble(array('controller' => 'login', 'action' => 'lost-password'), 'kwf_user'); $this->view->redirects = array(); $users = Zend_Registry::get('userModel'); foreach ($users->getAuthMethods() as $k => $auth) { if ($auth instanceof Kwf_User_Auth_Interface_Redirect && $auth->showInBackend()) { $url = $this->getFrontController()->getRouter()->assemble(array('controller' => 'backend-login', 'action' => 'redirect'), 'kwf_user'); $label = $auth->getLoginRedirectLabel(); $this->view->redirects[] = array('url' => $url, 'authMethod' => $k, 'redirect' => $_SERVER['REQUEST_URI'], 'name' => Kwf_Trl::getInstance()->trlStaticExecute($label['name']), 'icon' => isset($label['icon']) ? '/assets/' . $label['icon'] : false, 'formOptions' => Kwf_User_Auth_Helper::getRedirectFormOptionsHtml($auth->getLoginRedirectFormOptions())); } } parent::indexAction(); }
public function testHtml() { $html = $this->_root->getComponentById(2100)->render(); $doc = new DOMDocument(); $doc->strictErrorChecking = FALSE; $doc->loadHTML($html); $xml = simplexml_import_dom($doc); $img = $xml->xpath("//img"); $this->assertEquals(3, count($img)); $this->assertEquals(100, (string) $img[0]['width']); $this->assertEquals(100, (string) $img[0]['height']); $src = (string) $img[1]['src']; $this->assertTrue(!!preg_match('#/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m)); $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]); $this->assertEquals('image/png', $o['mimeType']); $im = new Imagick($o['file']); $this->assertEquals(16, $im->getImageWidth()); $this->assertEquals(16, $im->getImageHeight()); $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Composite_Images_Image_UploadsModel')->getRow(1)->getFileSource(), array(16, 16, 'cover' => true)), file_get_contents($o['file'])); }
private function _assert($componentId, $smallImage, $largeImage) { $html = $this->_root->getComponentById($componentId)->render(); $doc = new DOMDocument(); $doc->strictErrorChecking = FALSE; $doc->loadHTML($html); $xml = simplexml_import_dom($doc); $img = $xml->xpath("//img"); $this->assertEquals(1, count($img)); $this->assertEquals($smallImage['width'], (string) $img[0]['width']); $this->assertEquals($smallImage['height'], (string) $img[0]['height']); $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m)); $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]); $this->assertEquals($smallImage['mimeType'], $o['mimeType']); $im = new Imagick(); $im->readImageBlob($o['contents']); $this->assertEquals($smallImage['width'], $im->getImageWidth()); $this->assertEquals($smallImage['height'], $im->getImageHeight()); $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow($smallImage['uploadId'])->getFileSource(), array('width' => $smallImage['width'], 'height' => $smallImage['height'], 'cover' => true), $smallImage['uploadId']), $o['contents']); $a = $xml->xpath("//a"); $this->assertEquals(1, count($a)); $this->assertEquals('{"width":' . $largeImage['width'] . ',"height":' . $largeImage['height'] . ',"style":"CenterBox","adaptHeight":true,"lightboxUrl":"\\/foo1\\/image"}', (string) $a[0]['data-kwc-lightbox']); $this->assertContains('/media/Kwc_Basic_ImageEnlarge_EnlargeTagWithoutSmall_TestComponent/1800-linkTag', (string) $a[0]['href']); $html = $this->_root->getComponentById($componentId . '-linkTag_imagePage')->render(); $doc = new DOMDocument(); $doc->strictErrorChecking = FALSE; $doc->loadHTML($html); $xml = simplexml_import_dom($doc); $img = $xml->xpath("//img"); $this->assertEquals(1, count($img)); $this->assertEquals($largeImage['width'], (string) $img[0]['width']); $this->assertEquals($largeImage['height'], (string) $img[0]['height']); $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m)); $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]); $this->assertEquals($largeImage['mimeType'], $o['mimeType']); $im = new Imagick(); $im->readImage($o['file']); $this->assertEquals($largeImage['width'], $im->getImageWidth()); $this->assertEquals($largeImage['height'], $im->getImageHeight()); $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow($largeImage['uploadId'])->getFileSource(), array($largeImage['width'], $largeImage['height'], 'cover' => true)), file_get_contents($o['file'])); }
public function getContentWidth() { $data = $this->_getImageDataOrEmptyImageData(); $s = $this->getConfiguredImageDimensions(); if ($s['width'] === self::CONTENT_WIDTH) { return $this->getMaxContentWidth(); } if ($data) { if (isset($data['image'])) { $s = Kwf_Media_Image::calculateScaleDimensions($data['image'], $s); } else { $s = Kwf_Media_Image::calculateScaleDimensions($data['file'], $s); } return $s['width']; } return 0; }
/** * Bestfit = false * width + height * -> no-crop * -> bigger * -> wider: scales down, crops left and right * -> higher: scales down, crops top and bottom * -> matches aspectratio: scales down * -> smaller * -> wider: scales up and crops left and right * -> higher: scales up and crops top and bottom * -> matches aspectratio: scale up * -> matches size: return original * -> crop * -> complete image: * -> bigger: scales down, crops dependently * -> smaller: scales up, crops dependently * -> matches size: return original * -> part of image * -> bigger: scales down * -> wider: scales down, crops left and right * -> higher: scales down, crops top and bottom * -> smaller: scales up * -> wider: scales down, crops left and right * -> higher: scales down, crops top and bottom * -> matches size: no changes * * width/height * -> no-crop * -> bigger: adjust to given size * -> smaller: scale up to given size * -> matches size: return original * -> crop * -> complete image: * -> bigger: scales down * -> smaller: scales up * -> matches size: return original * -> part of image * -> bigger: adjust to given size * -> smaller: scale up to given size */ public function testBestFitFalse() { //test: width + height, no-crop, bigger, matches aspectratio: scale down $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 100, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test: width + height, no-crop, bigger, wider: scale down, crops left and right $imageSize = array('width' => 200, 'height' => 100); $dimension = array('width' => 100, 'height' => 100, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 50, 'y' => 0, 'width' => 100, 'height' => 100))); //test: width + height, no-crop, bigger, higher: scale down, crop top and bottom $imageSize = array('width' => 200, 'height' => 400); $dimension = array('width' => 100, 'height' => 100, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 100, 'width' => 200, 'height' => 200))); //test: width + height, no-crop, smaller, matches aspectratio: scale up $imageSize = array('width' => 50, 'height' => 50); $dimension = array('width' => 100, 'height' => 100, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 50))); //test: width + height, no-crop, smaller, wider: scale up, crop left and right $imageSize = array('width' => 100, 'height' => 50); $dimension = array('width' => 200, 'height' => 200, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 25, 'y' => 0, 'width' => 50, 'height' => 50))); //test: width + height, no-crop, smaller, higher: scale up, crop top and bottom $imageSize = array('width' => 50, 'height' => 100); $dimension = array('width' => 200, 'height' => 200, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 25, 'width' => 50, 'height' => 50))); //test: width + height, crop, complete image, smaller: scales up $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 200, 'height' => 200, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test width + height, crop, complete image, bigger: scales down $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 50, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test width + height, crop, complete image, matches size: return original $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 200, 'height' => 200, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200), 'keepOriginal' => true)); //test: width + height, crop, part of image, smaller: scales up $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 50)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 50))); //test: width + height, crop, part of image, smaller, wider: scales up, crops left and right $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 25)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 12.5, 'y' => 0, 'width' => 25, 'height' => 25))); //test: width + height, crop, part of image, smaller, higher: scales up, crops top and bottom $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 25, 'height' => 50)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 12.5, 'width' => 25, 'height' => 25))); //test width + height, crop, part of image, bigger: scales down $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 50, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test: width + height, crop, part of image, bigger, wider: scales down, crops left and right $imageSize = array('width' => 300, 'height' => 300); $dimension = array('width' => 50, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 50, 'y' => 0, 'width' => 100, 'height' => 100))); //test: width + height, crop, part of image, bigger, higher: scales down, crops top and bottom $imageSize = array('width' => 300, 'height' => 300); $dimension = array('width' => 50, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 200)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 50, 'width' => 100, 'height' => 100))); //test width + height, crop, part of image, matches size: nothing $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test width, no-crop, bigger: adjust to given size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 0, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test height, no-crop, bigger: adjust to given size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 0, 'height' => 100, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test width, no-crop, smaller: scale up to given size $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 200, 'height' => 0, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test height, no-crop, smaller: scale up to given size $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 0, 'height' => 200, 'cover' => true); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test: width, crop, complete image, bigger: adjust size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 50, 'height' => 0, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test: height, crop, complete image, bigger: adjust size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 0, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 200, 'height' => 200))); //test width, crop, complete image, smaller: scale up to given size $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 200, 'height' => 0, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test: height, crop, complete image, smaller: scale up to given size $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 0, 'height' => 200, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100))); //test width, crop, complete image, match size: return original $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 100, 'height' => 0, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100), 'keepOriginal' => true)); //test: height, crop, complete image, match size: return original $imageSize = array('width' => 100, 'height' => 100); $dimension = array('width' => 0, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 100), 'keepOriginal' => true)); //test: width, crop, part of image, bigger: adjust size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 50, 'height' => 0, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 50)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 50, 'height' => 25, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 50))); //test: height, crop, part of image, bigger: adjust size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 0, 'height' => 50, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 25, 'height' => 50, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 100))); //test width, crop, part of image, smaller: scale up to given size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 100, 'height' => 0, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 100)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 100, 'height' => 200, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 50, 'height' => 100))); //test: height, crop, part of image, smaller: scale up to given size $imageSize = array('width' => 200, 'height' => 200); $dimension = array('width' => 0, 'height' => 100, 'cover' => true, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 50)); $ret = Kwf_Media_Image::calculateScaleDimensions($imageSize, $dimension); $this->assertEquals($ret, array('width' => 200, 'height' => 100, 'rotate' => 0, 'crop' => array('x' => 0, 'y' => 0, 'width' => 100, 'height' => 50))); }
private function _saveImageDimensions() { if (is_null($this->is_image)) { $size = null; if ($this->getFileSource() && is_file($this->getFileSource())) { $size = @getimagesize($this->getFileSource()); $rotation = Kwf_Media_Image::getExifRotation($this->getFileSource()); } if ($size) { $this->is_image = 1; $this->image_width = $size[0]; $this->image_height = $size[1]; $this->image_rotation = $rotation; } else { $this->is_image = 0; } $this->save(); } }
public function previewWithCropAction() { $previewWidth = 390; $previewHeight = 184; $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } //Scale dimensions $dimensions = array($previewWidth, $previewHeight, 'cover' => false); $cache = Kwf_Assets_Cache::getInstance(); $cacheId = 'previewLarge_' . $fileRow->id; if (!($output = $cache->load($cacheId))) { $output = array(); $output['contents'] = Kwf_Media_Image::scale($fileRow->getFileSource(), $dimensions, $fileRow->id); $output['mimeType'] = $fileRow->mime_type; $cache->save($output, $cacheId); } $imageOriginal = new Imagick($fileRow->getFileSource()); if ($this->_getParam('cropX') == null || $this->_getParam('cropY') == null || $this->_getParam('cropWidth') == null || $this->_getParam('cropHeight') == null) { //calculate default selection $dimension = $this->_getParam('dimension'); if (!$dimension) { Kwf_Media_Output::output($output); } $dimensions = Kwc_Abstract::getSetting($this->_getParam('class'), 'dimensions'); $dimension = $dimensions[$dimension]; if (!isset($dimension['width'])) { $dimension['width'] = 0; } if (!isset($dimension['height'])) { $dimension['height'] = 0; } if (!isset($dimension['cover'])) { $dimension['cover'] = false; } if ($dimension['width'] == Kwc_Abstract_Image_Component::USER_SELECT) { $dimension['width'] = $this->_getParam('width'); } if ($dimension['height'] == Kwc_Abstract_Image_Component::USER_SELECT) { $dimension['height'] = $this->_getParam('height'); } if (!$dimension['cover']) { Kwf_Media_Output::output($output); } if ($dimension['width'] == Kwc_Abstract_Image_Component::CONTENT_WIDTH) { Kwf_Media_Output::output($output); } if ($dimension['height'] == 0 || $dimension['width'] == 0) { Kwf_Media_Output::output($output); } $cropX = 0; $cropY = 0; $cropHeight = $imageOriginal->getImageHeight(); $cropWidth = $imageOriginal->getImageWidth(); if ($imageOriginal->getImageHeight() / $dimension['height'] > $imageOriginal->getImageWidth() / $dimension['width']) { // orientate on width $cropHeight = $dimension['height'] * $imageOriginal->getImageWidth() / $dimension['width']; $cropY = ($imageOriginal->getImageHeight() - $cropHeight) / 2; } else { // orientate on height $cropWidth = $dimension['width'] * $imageOriginal->getImageHeight() / $dimension['height']; $cropX = ($imageOriginal->getImageWidth() - $cropWidth) / 2; } } else { //Calculate values relative to original image size $factor = Kwf_Media_Image::getHandyScaleFactor($fileRow->getFileSource()); $cropX = $this->_getParam('cropX') * $factor; $cropY = $this->_getParam('cropY') * $factor; $cropWidth = $this->_getParam('cropWidth') * $factor; $cropHeight = $this->_getParam('cropHeight') * $factor; } // Calculate values relative to preview image $image = new Imagick(); $image->readImageBlob($output['contents']); $previewFactor = 1; if ($image->getImageWidth() == $previewWidth) { $previewFactor = $image->getImageWidth() / $imageOriginal->getImageWidth(); } else { if ($image->getImageHeight() == $previewHeight) { $previewFactor = $image->getImageHeight() / $imageOriginal->getImageHeight(); } } $cropX = floor($cropX * $previewFactor); $cropY = floor($cropY * $previewFactor); $cropWidth = floor($cropWidth * $previewFactor); $cropHeight = floor($cropHeight * $previewFactor); $draw = new ImagickDraw(); if ($this->_isLightImage($output)) { $draw->setFillColor('black'); } else { $draw->setFillColor('white'); } $draw->setFillOpacity(0.3); // if cropX == 0 or cropY == 0 no rectangle should be drawn, because it // can't be 0 wide on topmost and leftmost position so it will result in // a 1px line which is wrong //Top region if ($cropY > 0) { $draw->rectangle(0, 0, $image->getImageWidth(), $cropY); } //Left region if ($cropX > 0) { if ($cropY > 0) { $draw->rectangle(0, $cropY + 1, $cropX, $cropY + $cropHeight - 1); } else { $draw->rectangle(0, $cropY, $cropX, $cropY + $cropHeight - 1); } } //Right region if ($cropY > 0) { $draw->rectangle($cropX + $cropWidth, $cropY + 1, $image->getImageWidth(), $cropY + $cropHeight - 1); } else { $draw->rectangle($cropX + $cropWidth, $cropY, $image->getImageWidth(), $cropY + $cropHeight - 1); } //Bottom region $draw->rectangle(0, $cropY + $cropHeight, $image->getImageWidth(), $image->getImageHeight()); $image->drawImage($draw); $output['contents'] = $image->getImageBlob(); Kwf_Media_Output::output($output); }
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 downloadHandyAction() { $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } $scaleFactor = Kwf_Media_Image::getHandyScaleFactor($fileRow->getFileSource()); $outputParams = array('mimeType' => $fileRow->mime_type, 'downloadFilename' => $fileRow->filename . '.' . $fileRow->extension); $targetSize = array(600, 600, 'cover' => false); $image = Kwf_Media_Image::scale($fileRow->getFileSource(), $targetSize, $fileRow->id); $outputParams['contents'] = $image; Kwf_Media_Output::output($outputParams); }
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 testWithSmallImageUploadedHtml() { $html = $this->_root->getComponentById(1802)->render(); $doc = new DOMDocument(); $doc->strictErrorChecking = FALSE; $doc->loadHTML($html); $xml = simplexml_import_dom($doc); $img = $xml->xpath("//img"); $this->assertEquals(1, count($img)); $this->assertEquals(10, (string) $img[0]['width']); $this->assertEquals(10, (string) $img[0]['height']); $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m)); $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]); $this->assertEquals('image/gif', $o['mimeType']); $im = new Imagick(); if (isset($o['contents'])) { $contents = $o['contents']; } else { $contents = file_get_contents($o['file']); } $im->readImageBlob($contents); $this->assertEquals(10, $im->getImageWidth()); $this->assertEquals(10, $im->getImageHeight()); $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow('2')->getFileSource(), array(10, 10, 'cover' => true), 2), $contents); $a = $xml->xpath("//a"); $this->assertEquals(1, count($a)); $this->assertEquals('{"width":210,"height":70,"style":"CenterBox","adaptHeight":true,"lightboxUrl":"\\/foo3\\/image"}', (string) $a[0]['data-kwc-lightbox']); $this->assertContains('/media/Kwc_Basic_ImageEnlarge_EnlargeTag_TestComponent/1802-linkTag/', (string) $a[0]['href']); $html = $this->_root->getComponentById('1802-linkTag_imagePage')->render(); $doc = new DOMDocument(); $doc->strictErrorChecking = FALSE; $doc->loadHTML($html); $xml = simplexml_import_dom($doc); $img = $xml->xpath("//img"); $this->assertEquals(1, count($img)); $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m)); $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]); $this->assertEquals('image/gif', $o['mimeType']); $im = new Imagick(); if (isset($o['contents'])) { $contents = $o['contents']; } else { $contents = file_get_contents($o['file']); } $im->readImageBlob($contents); // crop is set, because show_selection is set and else compare will fail $content = Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow('2')->getFileSource(), array(210, 70, 'cover' => false, 'crop' => array('height' => 70, 'width' => 210, 'x' => 0, 'y' => 0))); $image = new Imagick(); $image->readimageblob($content); $this->assertEquals(210, $im->getImageWidth()); $this->assertEquals(70, $im->getImageHeight()); $this->assertEquals($content, $contents); }
/** * Returns an image with a size which should be good to work with. * Acutally this is a 600x600 max-width. If it's smaller in both dimensions * it will keep it's original size. */ public static function getHandyScaleFactor($originalPath) { if (!file_exists($originalPath)) { return 1; } $targetSize = array(600, 600, 'cover' => false); $original = @getimagesize($originalPath); if (abs(self::getExifRotation($originalPath)) == 90) { $original = array($original[1], $original[0]); } $original['width'] = $original[0]; $original['height'] = $original[1]; $target = Kwf_Media_Image::calculateScaleDimensions($originalPath, $targetSize); if ($original['width'] <= $target['width'] && $original['height'] <= $target['height']) { return 1; } else { return $original['width'] / $target['width']; } }
/** * 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; }