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(); } }
/** * 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 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 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 function getImageDimensions() { $dimension = $this->_getSetting('dimension'); if ($dimension['width'] === self::CONTENT_WIDTH) { $dimension['width'] = $this->getContentWidth(); } $parentDimension = $this->_getImageComponent()->getConfiguredImageDimensions(); if (isset($parentDimension['crop'])) { $dimension['crop'] = $parentDimension['crop']; } $data = $this->getImageData(); return Kwf_Media_Image::calculateScaleDimensions($data['file'], $dimension); }
public function load($row, $role, $info) { $ret = null; $uploadRow = $row->getParentRow($this->getRuleKey()); if ($uploadRow) { $width = $this->getWidth() - 10; $size = array('width' => $width, 'height' => $this->getMaxHeight(), 'cover' => false); $size = Kwf_Media_Image::calculateScaleDimensions($uploadRow->getFileSource(), $size); $ret = array('previewUrl' => Kwf_Media::getUrl(get_class($this), $uploadRow->id, 'preview-' . $width . '-' . $this->getMaxHeight(), $uploadRow), 'previewHeight' => $size['height'], 'previewWidth' => $size['width']); if ($this->getShowHoverImage()) { $size = array('width' => 250, 'height' => 250, 'cover' => false); $size = Kwf_Media_Image::calculateScaleDimensions($uploadRow->getFileSource(), $size); $ret['hoverUrl'] = Kwf_Media::getUrl(get_class($this), $uploadRow->id, 'hover', $uploadRow); $ret['hoverHeight'] = $size['height']; $ret['hoverWidth'] = $size['width']; } } return $ret; }
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(); }
/** * 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']; } }
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))); }
/** * 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($original) { $targetSize = array(600, 600, 'cover' => false); if (is_string($original)) { if (!file_exists($original)) { return 1; } $size = getimagesize($original); $original = array('width' => $size[0], 'height' => $size[1], 'rotation' => self::getExifRotation($original)); } $target = Kwf_Media_Image::calculateScaleDimensions($original, $targetSize); if (abs($original['rotation']) == 90) { $original = array('width' => $original['height'], 'height' => $original['width']); } if ($original['width'] <= $target['width'] && $original['height'] <= $target['height']) { return 1; } else { return $original['width'] / $target['width']; } }
protected function _createItemByRow($row) { $truncateHelper = new Kwf_View_Helper_Truncate(); $primaryKey = $this->_primaryKey; $r = array(); if (!isset($r[$primaryKey]) && isset($row->{$primaryKey})) { $r[$primaryKey] = $row->{$primaryKey}; } if (!$this->_labelField && $row instanceof Kwf_Model_Row_Interface) { $r['label'] = $row->__toString(); } else { if ($this->_labelField) { $r['label'] = $row->{$this->_labelField}; } else { throw new Kwf_Exception("You have to set _labelField in the ImageGrid Controller"); } } if (!empty($r['label'])) { $r['label_short'] = $r['label']; } if (!empty($r['label_short']) && $this->_maxLabelLength) { $r['label_short'] = $truncateHelper->truncate($r['label_short'], $this->_maxLabelLength, '…', true, false); } $imageRef = $this->_getImageReference(); $hashKey = Kwf_Util_Hash::hash($row->{$imageRef['column']}); $r['src'] = '/kwf/media/upload/preview?uploadId=' . $row->{$imageRef['column']} . '&hashKey=' . $hashKey . '&size=imageGrid'; $r['src_large'] = '/kwf/media/upload/preview?uploadId=' . $row->{$imageRef['column']} . '&hashKey=' . $hashKey . '&size=imageGridLarge'; if ($uploadRow = $row->getParentRow($this->_imageRule)) { $dim = Kwf_Media_Image::calculateScaleDimensions($uploadRow->getFileSource(), array(400, 400, 'cover' => false)); $r['src_large_width'] = $dim['width']; $r['src_large_height'] = $dim['height']; } foreach ($this->_additionalDataFields as $f) { $r[$f] = $row->{$f}; } return $r; }