public function getCatResizedImage($cat, $width, $height = null, $quality = 100) { if (!$cat->getThumbnail()) { return false; } $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $cat->getThumbnail(); if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "cache" . DS . "cat_resized" . DS . $cat->getThumbnail(); // Because clean Image cache function works in this folder only //if (! file_exists ( $imageResized ) && file_exists ( $imageUrl ) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) : $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(true); // ep $imageObj->quality($quality); $imageObj->keepTransparency(true); // png $imageObj->backgroundColor(array(255, 255, 255)); $imageObj->resize($width, $height); $imageObj->save($imageResized); //endif; if (file_exists($imageResized)) { return Mage::getBaseUrl('media') . "/catalog/category/cache/cat_resized/" . $cat->getThumbnail(); } else { return $this->getImageUrl(); } }
public function getRetouchedResizedImage($image, $x, $y) { if (!$image || !$x || !$y) { return false; } $imageUrl = Mage::getBaseDir('media') . DS . "surgery" . DS . "retouched" . DS . $image; if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . "surgery" . DS . "retouched" . DS . "resized" . DS . $image; if (file_exists($imageResized)) { return Mage::getBaseUrl('media') . "surgery" . DS . "retouched" . DS . "resized" . DS . $image; } if (!file_exists($imageResized) && file_exists($imageUrl)) { try { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->resize($x, $y); $imageObj->save($imageResized); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } } return Mage::getBaseUrl('media') . "surgery" . DS . "retouched" . DS . "resized" . DS . $image; }
public function resizeImg($fileName, $width, $height = null) { $baseURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); $imageURL = $baseURL . '/' . 'magiccart/blog' . '/' . $fileName; $basePath = Mage::getBaseDir('media'); $imagePath = $basePath . DS . 'magiccart/blog/' . str_replace('/', DS, $fileName); $extra = $width . 'x' . $height; $newPath = Mage::getBaseDir('media') . DS . 'magiccart/blog' . DS . "cache" . DS . $extra . '/' . str_replace('/', DS, $fileName); //if width empty then return original size image's URL if ($width != '' && $height != '') { //if image has already cache then just return URL if (file_exists($imagePath) && is_file($imagePath) && !file_exists($newPath)) { $imageObj = new Varien_Image($imagePath); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepTransparency(true); $imageObj->keepFrame(FALSE); $imageObj->quality(100); //$width, $height - sizes you need (Note: when keepAspectRatio(TRUE), height would be ignored) $imageObj->resize($width, $height); $imageObj->save($newPath); } $cacheURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "magiccart/blog" . '/' . "cache" . '/' . $extra . '/' . $fileName; } else { $cacheURL = $imageURL; } return $cacheURL; }
public function resizeImg($fileName, $width = '', $height = null) { $imageURL = $this->getBaseTmpMediaUrl() . $fileName; $imagePath = $this->getBaseTmpMediaPath() . str_replace('/', DS, $fileName); $extra = $width . 'x' . $height; $newPath = $this->getBaseTmpMediaPath() . "cache" . DS . $extra . str_replace('/', DS, $fileName); //if width empty then return original size image's URL if ($width != '' && $height != '') { //if image has already resized then just return URL if (file_exists($imagePath) && is_file($imagePath) && !file_exists($newPath)) { $imageObj = new Varien_Image($imagePath); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepTransparency(true); $imageObj->keepFrame(FALSE); $imageObj->quality(100); //$width, $height - sizes you need (Note: when keepAspectRatio(TRUE), height would be ignored) $imageObj->resize($width, $height); $imageObj->save($newPath); } $resizedURL = $this->getBaseTmpMediaUrl() . "cache" . '/' . $extra . '/' . $fileName; } else { $resizedURL = $imageURL; } return $resizedURL; }
public function getPostImage($width = false, $height = false) { $img = $this->getData('post_image'); if (empty($img)) { return false; } $imgDir = dirname($img); $imgFile = basename($img); if (!$width) { return $img; } //if(!$height) $height = $width; $imageUrl = Mage::getBaseDir('media') . DS . $img; if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . $imgDir . "/resized/" . $width . "x" . $height . DS . $imgFile; if (file_exists($imageResized)) { return $imgDir . "/resized/" . $width . "x" . $height . DS . $imgFile; } $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->resize($width, $height); $imageObj->save($imageResized); return $imgDir . "/resized/" . $width . "x" . $height . DS . $imgFile; }
public function resizeImage($image, $type = "l", $width, $height, $storeid = null) { $image = str_replace("/", DS, $image); $_imageUrl = Mage::getBaseDir('media') . DS . $image; if ($storeid === null) { $storeid = Mage::app()->getStore()->getId(); } if (!$storeid) { $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . $type . DS . $image; } else { $image2 = str_replace("blog" . DS, "", $image); $image = "blog" . DS . $storeid . DS . $image2; $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . $type . DS . "blog" . DS . $storeid . DS . $image2; } if (!file_exists($imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); $imageObj->quality(100); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(false); $imageObj->keepFrame(false); $imageObj->keepTransparency(true); $imageObj->resize($width, $height); $imageObj->save($imageResized); } return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'resized/' . "{$type}/" . str_replace(DS, "/", $image); }
public function resizeImage($image, $width, $height) { $image = str_replace("ves_layerslider/upload/", "", $image); $image = str_replace("/", DS, $image); $_imageUrl = Mage::helper("ves_layerslider")->getImageBaseDir() . $image; $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . "{$width}x{$height}" . DS . $image; $quality = $this->getConfig("resize_quality"); if (!file_exists($imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); if ($quality) { $imageObj->quality($quality); } else { $imageObj->quality(100); } $imageObj = new Varien_Image($_imageUrl); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(false); $imageObj->keepTransparency(true); $imageObj->resize($width, $height); $imageObj->save($imageResized); } return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'resized/' . "{$width}x{$height}/" . str_replace(DS, "/", $image); }
public function resize($image, $width, $height) { $image = str_replace("/", DS, $image); $_imageUrl = Mage::getBaseDir('media') . DS . $image; $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . "{$width}x{$height}" . DS . $image; if (!file_exists($imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); $imageObj->quality(100); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(false); $imageObj->keepTransparency(true); $imageObj->resize($width, $height); $imageObj->save($imageResized); } return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'resized/' . "{$width}x{$height}/" . str_replace(DS, "/", $image); }
protected function _getResizedImage($file, $width, $height, $resizeMode = 'cover') { if (empty($file)) { return false; } $width = (int) $width; $height = (int) $height; $imagePath = $this->getImageFullPath($file); $imageFileResized = $width . '_' . $height . '_' . $resizeMode . DS . $file; $imageFileResizedFullPath = $this->getImageCacheFullPath($imageFileResized); if (!file_exists($imageFileResizedFullPath) && file_exists($imagePath) || file_exists($imagePath) && filemtime($imagePath) > filemtime($imageFileResizedFullPath)) { $finalAspectRatio = $width / $height; $imageObj = new Varien_Image($imagePath); $imageObj->backgroundColor(array(255, 255, 255)); $originalWidth = (int) $imageObj->getOriginalWidth(); $originalHeight = (int) $imageObj->getOriginalHeight(); $originalAspectRatio = $originalWidth / $originalHeight; if ($resizeMode == 'cover') { if ($originalAspectRatio > $finalAspectRatio) { $cropWidth = $finalAspectRatio * $originalHeight; $widthDiff = $originalWidth - $cropWidth; $cropX = (int) ($widthDiff / 2); $imageObj->crop(0, $cropX, $cropX, 0); } else { $cropHeight = (int) ($originalWidth / $finalAspectRatio); $heightDiff = $originalHeight - $cropHeight; $cropY = (int) ($heightDiff / 2); $imageObj->crop($cropY, 0, 0, $cropY); } } else { $imageObj->constrainOnly(false); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(true); } $imageObj->resize($width, $height); $imageObj->quality(100); $imageObj->save($imageFileResizedFullPath); } $imageCacheUrl = $this->getImageCacheUrl($imageFileResized); if (file_exists($imageFileResizedFullPath)) { return $imageCacheUrl; } return false; }
public function resizeImage($image, $width, $height) { $image = str_replace("/", DS, $image); $_imageUrl = Mage::getBaseDir('media') . DS . $image; $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . "{$width}x{$height}" . DS . $image; $quality = $this->getConfig("resize_quality"); if (!file_exists($imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); $imageObj->quality(100); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(false); $imageObj->keepTransparency(true); $imageObj->resize($width, $height); if ($quality) { $imageObj->quality($quality); } $imageObj->save($imageResized); } return 'resized/' . "{$width}x{$height}/" . str_replace(DS, "/", $image); }
/** * Run script * */ public function run() { if (!$this->getArg('d')) { echo $this->usageHelp(); return; } /** @var $iterator SplFileObject[] */ $iterator = new DirectoryIterator($this->getArg('d')); $start = microtime(true); $i = 0; $max = 0; do { foreach ($iterator as $file) { if (!$file->isDir()) { $image = new Varien_Image($this->getArg('d') . DS . $file->getFilename(), Varien_Image_Adapter::ADAPTER_IM); $image->keepFrame(true); $image->keepAspectRatio(true); $image->keepTransparency(true); $image->backgroundColor(array(255, 255, 255)); $image->resize(186, 500); $image->setWatermarkImageOpacity(30); $image->setWatermarkPosition(Varien_Image_Adapter_Abstract::POSITION_TOP_LEFT); $image->setWatermarkHeigth(100); $image->setWatermarkWidth(100); $image->quality(80); $watermark = $this->getArg('d') . DS . 'watermark' . DS . 'watermark.png'; if (is_readable($watermark)) { $image->watermark($watermark); } $image->save($this->getArg('d') . DS . 'result', $file->getFilename()); } } } while ($i++ < $max); $endMem = memory_get_usage(true); $end = microtime(true); echo "Duration in seconds: " . ($end - $start) . PHP_EOL; echo "Memory usage in MB: " . $endMem / 1024 / 1024 . PHP_EOL; }
public function resizeImage($image, $width = 100, $height = 100, $qualtity = 100) { if ($width == 0 || $height == 0) { return Mage::getBaseUrl("media") . $image; } $media_base_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); $image = str_replace($media_base_url, "", $image); $media_base_url = str_replace("https://", "http://", $media_base_url); $image = str_replace($media_base_url, "", $image); $_imageUrl = Mage::getBaseDir('media') . DS . $image; $_imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . (int) $width . "x" . (int) $height . DS . $image; if (!file_exists($_imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); $imageObj->quality($qualtity); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepTransparency(true); $imageObj->keepFrame(FALSE); $imageObj->resize($width, $height); $imageObj->save($_imageResized); } return Mage::getBaseUrl("media") . "resized/" . (int) $width . "x" . (int) $height . "/" . $image; }
public function getSmallImageFile($fileOrig, $smallPath, $newFileName) { try { $image = new Varien_Image($fileOrig); $origHeight = $image->getOriginalHeight(); $origWidth = $image->getOriginalWidth(); // settings $image->keepAspectRatio(true); $image->keepFrame(true); $image->keepTransparency(true); $image->constrainOnly(false); $image->backgroundColor(array(255, 255, 255)); $image->quality(90); $width = null; $height = null; if (Mage::app()->getStore()->isAdmin()) { if ($origHeight > $origWidth) { $height = $this->getImagesThumbnailsSize(); } else { $width = $this->getImagesThumbnailsSize(); } } else { $configWidth = $this->getImagesThumbnailsSize(); $configHeight = $this->getImagesThumbnailsSize(); if ($origHeight > $origWidth) { $height = $configHeight; } else { $width = $configWidth; } } $image->resize($width, $height); $image->constrainOnly(true); $image->keepAspectRatio(true); $image->keepFrame(false); //$image->display(); $image->save($smallPath, $newFileName); } catch (Exception $e) { } }
public function getResizedImage($image, $width = null, $height = null, $quality = 100) { $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $image; if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "product" . DS . "cache" . DS . "cat_resized" . DS . $image; if (!file_exists($imageResized) && file_exists($imageUrl) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(true); //$imageObj->keepAspectRatio ( true ); $imageObj->keepFrame(false); $imageObj->quality($quality); if ($width) { $imageObj->resize($width, $height ? $height : null); } $imageObj->save($imageResized); } if (file_exists($imageResized)) { return $image; } else { return false; } }
public function getResizedImage($width = null, $height = null, $quality = 100, $image = NULL) { if (!$image) { return false; } $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $image; if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "cat_resized" . DS . $image; if (!file_exists($imageResized) && file_exists($imageUrl) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->keepTransparency(true); $imageObj->resize($width, $height); $imageObj->save($imageResized); } if (file_exists($imageResized)) { return Mage::getBaseUrl('media') . "catalog/category/cat_resized/" . $image; } else { return $imageUrl; } }
public function getResizedImage($width, $height) { if (!$this->getThumbnail()) { return false; } $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $this->getThumbnail(); if (!is_file($imageUrl)) { return false; } $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized" . DS . $width . '-' . $height . '-' . $this->getThumbnail(); if (file_exists($imageResized)) { return dirname($this->getImageUrl()) . "/resized/" . $width . '-' . $height . '-' . $this->getThumbnail(); } if (!file_exists($imageResized) && file_exists($imageUrl)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->resize($width, $height); $imageObj->save($imageResized); } return dirname($this->getImageUrl()) . "/resized/" . $width . '-' . $height . '-' . $this->getThumbnail(); }
public function saveAction() { if ($data = $this->getRequest()->getPost()) { if ($data['filename']['delete'] == 1) { $data['filename'] = ''; } elseif (is_array($data['filename'])) { $data['filename'] = $data['filename']['value']; } $file = new Varien_Io_File(); $baseDir = Mage::getBaseDir(); $mediaDir = $baseDir . DS . 'media'; $imageDir = $mediaDir . DS . 'bsimages'; $thumbimageyDir = $mediaDir . DS . 'bsimages' . DS . 'thumbs'; if (!is_dir($imageDir)) { $imageDirResult = $file->mkdir($imageDir, 0777); } if (!is_dir($thumbimageyDir)) { $thumbimageDirResult = $file->mkdir($thumbimageyDir, 0777); } //$thumbimageDirResult = $file->mkdir($thumbimageyDir); if (isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') { try { /* Starting upload */ $uploader = new Varien_File_Uploader('filename'); // Any extention would work $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(false); // Set the file upload mode // false -> get the file directly in the specified folder // true -> get the file in the product like folders // (file.jpg will go in something like /media/f/i/file.jpg) $uploader->setFilesDispersion(false); // We set media as the upload dir //$path = Mage::getBaseDir('media') . DS ; $path = $imageDir . DS; $result = $uploader->save($path, $_FILES['filename']['name']); ############################################################################### // actual path of image $imageUrl = Mage::getBaseDir('media') . DS . "bsimages" . DS . $_FILES['filename']['name']; // path of the resized image to be saved // here, the resized image is saved in media/resized folder $imageResized = Mage::getBaseDir('media') . DS . "bsimages" . DS . "thumbs" . DS . $_FILES['filename']['name']; // resize image only if the image file exists and the resized image file doesn't exist // the image is resized proportionally with the width/height 135px if (!file_exists($imageResized) && file_exists($imageUrl)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepFrame(FALSE); $imageObj->quality(100); $imageObj->resize(80, 50); $imageObj->save($imageResized); } ################################################################################# $data['filename'] = $result['file']; } catch (Exception $e) { $data['filename'] = $_FILES['filename']['name']; } } $model = Mage::getModel('imageslider/imageslider'); $model->setData($data)->setId($this->getRequest()->getParam('id')); try { if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) { $model->setCreatedTime(now())->setUpdateTime(now()); } else { $model->setUpdateTime(now()); } //$model->setStores(implode(',',$data['stores'])); /*if (isset($data['category_ids'])){ $model->setCategories(implode(',',array_unique(explode(',',$data['category_ids'])))); }*/ $model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('imageslider')->__('Banner Image was successfully saved')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('imageslider')->__('Unable to save Banner Image')); $this->_redirect('*/*/'); }
function convertImage($OriginalUrl, $ResizedlUrl = null) { $width = $this->width; $height = $this->height; if (!is_file($OriginalUrl)) { return false; } if (!$ResizedlUrl) { $ResizedlUrl = $OriginalUrl; } $imageObj = new Varien_Image($OriginalUrl); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(true); // force Frame $imageObj->quality($this->quality); $imageObj->keepTransparency(true); // keep Transparency with image png $imageObj->backgroundColor(array(255, 255, 255)); $imageObj->resize($width, $height); $imageObj->save($ResizedlUrl); }
/** * @param $path */ protected function resize_image($path) { $image = new Varien_Image($path); $image->constrainOnly(true); $image->keepAspectRatio(true); $image->keepFrame(false); $image->keepTransparency(true); $image->setImageBackgroundColor(false); $image->backgroundColor(false); $image->quality(100); $image->setWatermarkImageOpacity(0); $image->resize(120, 120); $image->save($path); }
public function resizeImg($img, $width, $height = false, $customerId = false) { if (!isset($customerId)) { $customerId = $this->_customerId; } $_media_dir = Mage::getBaseDir('media') . DS . 'albums' . DS . $customerId . DS; $imgSize = getimagesize(Mage::getBaseDir('media') . DS . 'albums' . DS . $customerId . DS . $img); // real image sizes $imgWidth = $imgSize[0]; $imgHeight = $imgSize[1]; if ($imgWidth > $imgHeight) { $imgProp = $imgWidth / $imgHeight; $newWidth = $width; $newHeight = $width / $imgProp; } elseif ($imgWidth < $imgHeight) { $imgProp = $imgHeight / $imgWidth; $newWidth = $width / $imgProp; $newHeight = $width; } elseif ($imgWidth == $imgHeight) { $newWidth = $newHeight = $width; } $cache_dir = $_media_dir . 'cache' . DS . $width . DS; if (file_exists($_media_dir . $img)) { if (!is_dir($_media_dir . 'cache' . DS)) { mkdir($_media_dir . 'cache'); } elseif (!is_dir($cache_dir)) { mkdir($cache_dir); } $_image = new Varien_Image($_media_dir . $img); $_image->constrainOnly(FALSE); $_image->keepAspectRatio(TRUE); $_image->keepFrame(TRUE); $_image->keepTransparency(TRUE); $_image->backgroundColor(array(255, 255, 255)); $_image->setImageBackgroundColor(TRUE); $_image->quality(100); //$_image->resize($width, $height); $_image->resize($newWidth, $newHeight); $_image->save($cache_dir . $img); return Mage::getBaseUrl() . 'media/albums/' . $customerId . '/cache/' . $width . '/' . $img; } return false; }
public function resizeImage($image, $width, $height) { $image = str_replace("/", DS, $image); $_imageUrl = Mage::getBaseDir('media') . DS . $image; $imageResized = Mage::getBaseDir('media') . DS . "resized" . DS . "{$width}x{$height}" . DS . $image; if (!file_exists($imageResized) && file_exists($_imageUrl)) { $imageObj = new Varien_Image($_imageUrl); $imageObj->quality(100); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->resize($width, $height); $imageObj->save($imageResized); } return 'resized/' . "{$width}x{$height}/" . str_replace(DS, "/", $image); }
/** * Resize category images to display * * Return image url * * @return string */ public function getResizedImage($imagePath, $width, $height = null, $quality = 100) { $return = ''; $imageUrl = Mage::getBaseDir('media') . DS . 'catalog' . DS . "category" . DS . $imagePath; if (!$imagePath || !is_file($imageUrl)) { $return = false; } else { /** * Because clean Image cache function works in this folder only */ $imageResized = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . DS . "cache" . DS . "cat_resized" . DS . $width . $imagePath; if (!file_exists($imageResized) && file_exists($imageUrl) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(false); $imageObj->keepFrame(false); $imageObj->quality($quality); $imageObj->resize($width, $height); $imageObj->save($imageResized); } if (file_exists($imageResized)) { $return = Mage::getBaseUrl('media') . "catalog/product/cache/cat_resized/" . $width . $imagePath; } else { $return = $imagePath; } } return $return; }
private function _getResizedImage($Obj, $width, $height, $quality = 100) { if (is_object($Obj)) { if (!$Obj->getImage()) { $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "no_image.gif"; } else { $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $Obj->getImage(); } } else { if (!$Obj) { $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "no_image.gif"; } else { $imageUrl = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . $Obj; } } if (!is_file($imageUrl)) { return false; } $file = pathinfo($imageUrl); //Zend_Debug::dump($file);die; $imageName = $file['filename'] . "_" . $width . "x" . $height . "." . $file['extension']; $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "product" . DS . "cache" . DS . "cat_resized" . DS . $imageName; // Because clean Image cache function works in this folder only if (!file_exists($imageResized) && file_exists($imageUrl) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(false); $imageObj->keepAspectRatio(false); $imageObj->keepFrame(false); $imageObj->quality($quality); $imageObj->resize($width, $height); $imageObj->save($imageResized); } if (file_exists($imageResized)) { return Mage::getBaseUrl('media') . "/catalog/product/cache/cat_resized/" . $imageName; } elseif (file_exists($Obj->getImageUrl())) { return $Obj->getImageUrl(); } else { return $this->getSkinUrl('sm/megamenu/images/no_image.gif'); } }
/** * */ public function saveAction() { if ($this->getRequest()->getPost()) { try { $post_data = $this->getRequest()->getPost(); $slider_id = $this->getRequest()->getParam('id'); $slide_model = Mage::getModel('slider/slider'); $destFolder = rtrim(BP, '/\\') . '/media/ismslider/'; $timeMarker = time(); foreach ($post_data['slides'] as $slideId => $values) { if (!empty($values['deleteimage'])) { @unlink($destFolder . $values['deleteimage']); @unlink($destFolder . 'thumb-' . $values['deleteimage']); $post_data['slides'][$slideId]['image'] = ''; } } foreach ($_FILES['slides']['name'] as $slideId => $values) { if (empty($values['image'])) { continue; } if (!is_writable($destFolder)) { Mage::getSingleton('adminhtml/session')->addError('Destination folder is not writable or does not exists.'); continue; } $fileName = Varien_File_Uploader::getCorrectFileName($values['image']); $destFile = $destFolder . $timeMarker . '_' . $fileName; $result = move_uploaded_file($_FILES['slides']['tmp_name'][$slideId]['image'], $destFile); if ($result) { chmod($destFile, 0644); $imageProcessor = new Varien_Image($destFile); $imageProcessor->keepAspectRatio(true); $imageProcessor->keepFrame(true); $imageProcessor->keepTransparency(true); $imageProcessor->constrainOnly(false); $imageProcessor->backgroundColor(array(255, 255, 255)); $imageProcessor->quality(90); $imageProcessor->resize(172, 60); $imageProcessor->save($destFolder, 'thumb-' . $timeMarker . '_' . $fileName); $post_data['slides'][$slideId]['image'] = $timeMarker . '_' . $fileName; chmod($destFolder, 'thumb-' . $timeMarker . '_' . $fileName, 0644); } else { $post_data['slides'][$slideId]['image'] = ''; Mage::getSingleton('adminhtml/session')->addError('File ' . $fileName . ' was not uploaded.'); } } $slide_model->setId($slider_id)->setData($post_data); if ($slider_id !== null) { $slide_model->setModifiedTime(new Zend_Db_Expr('NOW()')); } else { $slide_model->setCreatedTime(new Zend_Db_Expr('NOW()')); } $slide_model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Slider was successfully saved')); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $slide_model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } $this->_redirect('*/*/'); }
/** * resize and crop image * @param string $fileName * @param string $width * @param string $height * @return string - resized image url */ public function adaptiveResize($fileName, $width, $height) { $folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); $imageURL = $folderURL . $fileName; $basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . '/' . $fileName; $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . '/' . "resized" . '/' . $fileName; //if width empty then return original size image's URL if ($width != '') { //if image has already resized then just return URL if (file_exists($basePath) && is_file($basePath) && !file_exists($newPath)) { $imageObj = new Varien_Image($basePath); $imageObj->constrainOnly(FALSE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->quality(95); $this->currentDimensions = array(); $this->currentDimensions['width'] = $imageObj->getOriginalWidth(); $this->currentDimensions['height'] = $imageObj->getOriginalHeight(); $this->newDimensions = array(); $this->newDimensions['newWidth'] = $imageObj->getOriginalWidth(); $this->newDimensions['newHeight'] = $imageObj->getOriginalHeight(); $this->adaptiveResizeDimensions($width, $height); $imageObj->resize($this->newDimensions['newWidth'], $this->newDimensions['newHeight']); if ($this->cropDimensions['x'] > 0 || $this->cropDimensions['y'] > 0) { //top, left, right, bottom $imageObj->crop($this->cropDimensions['y'], $this->cropDimensions['x'], intval($this->newDimensions['newWidth'] - $this->_adaptiveMaxWidth - $this->cropDimensions['x']), intval($this->newDimensions['newHeight'] - $this->_adaptiveMaxHeight - $this->cropDimensions['y'])); } $imageObj->save($newPath); } $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . '/' . $fileName; } else { $resizedURL = $imageURL; } return $resizedURL; }
protected function _initVarienImage($basePath, $imageName, $cacheDir, $mode, $root) { if ($mode == 'catalog') { if (!($width = (int) Mage::getStoreConfig('collpur/general/thumbnailsize'))) { $width = self::CATALOG_IMAGE_SIZE; } } else { if (!($width = (int) Mage::getStoreConfig('collpur/general/imagesize'))) { $width = self::PRODUCT_IMAGE_SIZE; } } try { $image = new Varien_Image($root . DS . $basePath); $image->keepAspectRatio(true); $image->keepTransparency(true); $image->keepFrame(false); $image->quality(90); if ($image->getOriginalWidth() <= $width) { $width = $image->getOriginalWidth(); } $image->resize($width, $image->getOriginalHeight()); $image->save($root . DS . $cacheDir, $imageName); } catch (Exception $e) { Mage::log($e->getMessage()); } return $cacheDir . DS . $imageName; }