protected function _entityImageUrl(Varien_Object $entity, $width, $height, $entityField) { $entityPath = $entity->getData($entityField); if (!$entityPath) { return null; } $dstPath = array('cache', Mage::app()->getStore()->getId(), $entityField, $width . 'x' . $height, $entityPath); $dstFile = $this->getBaseMediaPath() . DS . implode(DS, $dstPath); if (!file_exists($dstFile)) { $srcPath = array($entityPath); $srcFile = $this->getBaseMediaPath() . DS . implode(DS, $srcPath); if (!file_exists($srcFile)) { return null; } $image = new Varien_Image($srcFile); $image->keepAspectRatio(true); $image->keepTransparency(true); $image->keepFrame(true); $image->constrainOnly(false); $image->backgroundColor(array(255, 255, 255)); $image->resize($width, $height); $image->save($dstFile); } return $this->getBaseMediaUrl() . '/' . implode('/', $dstPath); }
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 resize($imageUrl, $width, $height) { if (!file_exists(Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized")) { mkdir(Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized", 0777, true); } $imageName = substr(strrchr($imageUrl, "/"), 1); if ('255,255,255' !== $this->getBackgroundColor(true)) { $imageName = $width . 'x' . $height . '/' . $this->getBackgroundColor(true) . '/' . $imageName; } else { $imageName = $width . 'x' . $height . '/' . $imageName; } $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized" . DS . $imageName; $imagePath = str_replace(Mage::getBaseUrl('media'), 'media/', $imageUrl); $imagePath = Mage::getBaseDir() . DS . str_replace("/", DS, $imagePath); if (!file_exists($imageResized) && file_exists($imagePath)) { $imageObj = new Varien_Image($imagePath); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(true); $imageObj->keepTransparency(true); $imageObj->backgroundColor($this->getBackgroundColor()); $imageObj->resize($width, $height); $imageObj->save($imageResized); } $imageUrl = Mage::getBaseUrl('media') . "catalog/category/resized/" . $imageName; return $imageUrl; }
public function getProfileImage() { if (isset($this->_seller['sstech_profileimage']) && ($_file_name = $this->_seller['sstech_profileimage'])) { $_media_dir = Mage::getBaseDir('media') . DS . 'customer' . DS; // Here i create a resize folder. for upload new category image $cache_dir = $_media_dir . 'resize' . DS; if (file_exists($cache_dir . $_file_name)) { $img = Mage::getBaseUrl('media') . 'customer' . DS . 'resize' . $_file_name; } elseif (file_exists($_media_dir . $_file_name)) { if (!is_dir($cache_dir)) { mkdir($cache_dir); } $_image = new Varien_Image($_media_dir . $_file_name); $_image->constrainOnly(false); $_image->keepAspectRatio(true); $_image->keepFrame(true); $_image->keepTransparency(true); $_image->backgroundColor(array(255, 255, 255)); $_image->resize(300, null); // change image height, width $_image->save($cache_dir . $_file_name); $img = Mage::getBaseUrl('media') . 'customer' . DS . 'resize' . $_file_name; } } if (!isset($img)) { $img = Mage::getBaseUrl('media') . "default_user.jpg"; } return $img; }
public function resizeImg($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) . DS . $fileName; $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "resized" . DS . $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(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(TRUE); $imageObj->backgroundColor(array(255, 255, 255)); // white background! $imageObj->resize($width, $height); $imageObj->save($newPath); } $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName; } else { $resizedURL = $imageURL; } return $resizedURL; }
public function getResizedUrl($imgUrl, $x, $y = NULL) { $imgPath = $this->splitImageValue($imgUrl, "path"); $imgName = $this->splitImageValue($imgUrl, "name"); /** * Path with Directory Seperator */ $imgPath = str_replace("/", DS, $imgPath); /** * Absolute full path of Image */ $imgPathFull = Mage::getBaseDir("media") . DS . $imgPath . DS . $imgName; /** * If Y is not set set it to as X */ $widht = $x; $y ? $height = $y : ($height = $x); /** * * Resize folder is widthXheight */ $resizeFolder = $widht . "X" . $height; /** * Image resized path will then be */ $imageResizedPath = Mage::getBaseDir("media") . DS . $imgPath . DS . $resizeFolder . DS . $imgName; /** * First check in cache i.e image resized path * If not in cache then create image of the width=X and height = Y */ if (!file_exists($imageResizedPath) && file_exists($imgPathFull)) { $imageObj = new Varien_Image($imgPathFull); $imageObj->constrainOnly(true); // image picture will not be bigger, than it was $imageObj->keepAspectRatio(true); // image picture width/height will not be distorted $imageObj->keepFrame(true); // image will have dimensions, set in $width/$height $imageObj->keepTransparency(true); $imageObj->backgroundColor(array(255, 255, 255)); $imageObj->resize($widht, $height); $imageObj->save($imageResizedPath); } /** * Else image is in cache replace the Image Path with / for http path. */ $imgUrl = str_replace(DS, "/", $imgPath); /** * Return full http path of the image */ return Mage::getBaseUrl("media") . $imgUrl . "/" . $resizeFolder . "/" . $imgName; }
/** * if there's no thumb for the image, we make it * * @param string $imageName * @param int $size * * @return string */ public function resize($imageName, $size) { $helper = Mage::helper('adjicon'); $fileName = $helper->getThumbnailFileName($size, $imageName); if (!file_exists($this->_imagePath . $fileName)) { $image = new Varien_Image($this->_imagePath . $imageName); $image->keepFrame(true); $image->keepAspectRatio(true); $image->keepTransparency(true); $image->backgroundColor(array(255, 255, 255)); $image->resize($size); $image->save(null, $fileName); } return $fileName; }
public function saveAction() { if ($this->getRequest()->getPost()) { try { $postData = $this->getRequest()->getPost(); $imageModel = Mage::getModel('aitcg/mask'); if (isset($_FILES['filename']['name']) and file_exists($_FILES['filename']['tmp_name'])) { $uploader = new Varien_File_Uploader('filename'); $uploader->setAllowedExtensions(array('png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $path = $imageModel->getImagesPath(); $uploader->save($path, preg_replace('/[^A-Za-z\\d\\.]/', '_', $_FILES['filename']['name'])); $postData['filename'] = $uploader->getUploadedFileName(); } $imageModel->load($this->getRequest()->getParam('imgid'))->setName($postData['name'])->setResize($postData['resize'])->setCategoryId($this->getRequest()->getParam('id')); if (isset($postData['filename'])) { if ($imageModel->getFilename()) { $fullPath = $imageModel->getImagesPath() . $imageModel->getFilename(); @unlink($fullPath); $fullPath = $imageModel->getImagesPath() . 'preview' . DS . $imageModel->getFilename(); @unlink($fullPath); } $imageModel->setFilename($postData['filename']); $thumb = new Varien_Image($imageModel->getImagesPath() . $imageModel->getFilename()); $thumb->open(); $thumb->keepAspectRatio(true); $thumb->keepFrame(true); $thumb->backgroundColor(array(255, 255, 255)); #$thumb->keepTransparency(true); $thumb->resize(135); $thumb->save($imageModel->getImagesPath() . 'preview' . DS . $imageModel->getFilename()); $imageModel->createInvertMask(); } $imageModel->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved')); Mage::getSingleton('adminhtml/session')->setImageData(false); $this->_redirect('*/*/', array('id' => $this->getRequest()->getParam('id'))); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setImageData($this->getRequest()->getPost()); $this->_redirect('*/*/edit', array('imgid' => $this->getRequest()->getParam('imgid'), 'id' => $this->getRequest()->getParam('id'))); return; } } $this->_redirect('*/*/', array('id' => $this->getRequest()->getParam('id'))); }
public function setFilenameWithUnlink($filename) { if ($this->getFilename() && $this->getFilename() != $filename) { $fullPath = $this->getImagesPath() . $this->getFilename(); @unlink($fullPath); $fullPath = $this->getImagesPath() . 'preview' . DS . $this->getFilename(); @unlink($fullPath); } $this->setFilename($filename); $thumb = new Varien_Image($this->getImagesPath() . $this->getFilename()); $thumb->open(); $thumb->keepAspectRatio(true); $thumb->keepFrame(true); $thumb->backgroundColor(array(255, 255, 255)); #$thumb->keepTransparency(true); $thumb->resize(135); $thumb->save($this->getImagesPath() . 'preview' . DS . $this->getFilename()); }
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 resize($width = 100, $height = null) { $height = $height ? $height : $width; $this->createCacheFolder($width, $height); $orgFolder = $this->getOrgFolder(); $cacheSizeFolder = $this->getCacheSizeFolder($width, $height); $imageFile = $this->_cat->getImage(); if (!file_exists($cacheSizeFolder . DS . $imageFile)) { if (file_exists($orgFolder . DS . $imageFile)) { $fileImg = new Varien_Image($orgFolder . DS . $imageFile); $fileImg->keepAspectRatio(true); $fileImg->keepFrame(true); $fileImg->keepTransparency(true); $fileImg->constrainOnly(false); $fileImg->backgroundColor(array(255, 255, 255)); $fileImg->resize($width, $height); $fileImg->save($cacheSizeFolder . DS . $imageFile, null); } } return Mage::getBaseUrl('media') . 'catalog/category/cache/' . $this->_cat->getId() . '/' . $width . 'x' . $height . '/' . $imageFile; }
public function resize($imageUrl, $width, $height) { if (!file_exists(Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized")) { mkdir(Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized", 0777); } $imageName = substr(strrchr($imageUrl, "/"), 1); $imageName = $width . '_' . $height . '_' . $imageName; $imageResized = Mage::getBaseDir('media') . DS . "catalog" . DS . "category" . DS . "resized" . DS . $imageName; $dirImg = Mage::getBaseDir() . str_replace("/", DS, strstr($imageUrl, '/media')); if (!file_exists($imageResized) && file_exists($dirImg)) { $imageObj = new Varien_Image($dirImg); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame(true); // $imageObj->keepTransparency(true); $imageObj->backgroundColor($this->getBackgroundColor()); $imageObj->resize($width, $height); $imageObj->save($imageResized); } $imageUrl = Mage::getBaseUrl('media') . "catalog/category/resized/" . $imageName; return $imageUrl; }
/** * 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; }
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); }
/** * Generate thumb image * @param $imageUrl * @param $imageResized * @return $this */ public function thumbImageObj($imageUrl, $imageResized) { $imageObj = new Varien_Image($imageUrl); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->keepTransparency(true); $imageObj->backgroundColor(array(255, 255, 255)); $imageObj->resize(200, 200); $imageObj->save($imageResized); }
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; }
/** * Generate product thumbnails * @param unknown_type $product */ public function generateThumb($product) { $productId = $product->getId(); $image = trim($product->getSmallImage()); if (empty($image)) { $productImagePath = Mage::getBaseDir("skin") . DS . 'frontend' . DS . 'base' . DS . 'default' . DS . 'images' . DS . 'catalog' . DS . 'product' . DS . 'placeholder' . DS . 'image.jpg'; } else { $productImagePath = Mage::getBaseDir("media") . DS . 'catalog' . DS . 'product' . DS . $image; } if (!file_exists($productImagePath)) { $productImagePath = Mage::getBaseDir("skin") . DS . 'frontend' . DS . 'base' . DS . 'default' . DS . 'images' . DS . 'catalog' . DS . 'product' . DS . 'placeholder' . DS . 'image.jpg'; } $productImageThumbPath = Mage::getBaseDir('media') . DS . "catalog" . DS . "product" . DS . "sb_thumb" . DS . $productId . '.jpg'; if (file_exists($productImageThumbPath)) { unlink($productImageThumbPath); } $imageResizedUrl = Mage::getBaseUrl("media") . DS . "catalog" . DS . "product" . DS . "sb_thumb" . DS . $productId . '.jpg'; $imageObj = new Varien_Image($productImagePath); $imageObj->constrainOnly(FALSE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->backgroundColor(array(255, 255, 255)); //$imageObj->keepTransparency(TRUE); $imageObj->resize(32, 32); $imageObj->save($productImageThumbPath); if (file_exists($productImageThumbPath)) { return true; } return false; }
public function saveAction() { $session = Mage::getSingleton('adminhtml/session'); if ($data = $this->getRequest()->getPost()) { try { $path = Mage::getBaseDir('media') . DS . 'productattachments'; if (isset($_FILES['category_image']['name']) && $_FILES['category_image']['name'] != '') { //echo '<pre>';print_r($_FILES['event_image']);exit; try { /* Starting upload */ $uploader = new Varien_File_Uploader('category_image'); // 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 $uploader->save($path, $_FILES['category_image']['name']); $varImg = new Varien_Image($path . DS . $_FILES['category_image']['name']); $varImg->constrainOnly(TRUE); $varImg->keepAspectRatio(FALSE); $varImg->keepFrame(TRUE); $varImg->keeptransparency(FALSE); $varImg->backgroundColor(array(255, 255, 255)); // WHITE BACKGROUND $image_name = $_FILES['category_image']['name']; $varImg->resize(400, 400); $varImg->save($path, $image_name); $data['category_image'] = 'productattachments' . DS . $image_name; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('productattachments')->__('Error: ' . $e->getMessage())); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } $id = $this->getRequest()->getParam('id'); // checking URL key if (!isset($data['category_url_key']) || !$data['category_url_key']) { $data['category_url_key'] = $data['category_name']; } $data['category_url_key'] = FME_Productattachments_Helper_Data::nameToUrlKey($data['category_url_key']); $model = Mage::getModel('productattachments/productcats')->setData($data)->setId($id); if ($model->isUrlKeyUsed()) { $session->addError($this->__('URL key is not unique within category store views')); $session->setKBaseCategoryData($data); $this->_redirect('*/*/edit', array('id' => $id)); return; } $model->save(); $session->addSuccess($this->__('Category was successfully saved')); //$session->setKBaseCategoryData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::logException($e); $session->addError($e->getMessage()); $session->setKBaseCategoryData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } else { $session->addError($this->__('Unable to find an category to save')); $this->_redirect('*/*/'); } }
/** * Retrieve thumbnail image url * * @param string $imageUrl * @param int $width * @param int $height * @return string|null */ public function getCustomSizeImageUrl($imageUrl, $width = 100, $height = 100) { $screenSize = $width . 'x' . $height; $customDir = $this->getMediaPath('custom' . DS . $screenSize); $this->_verifyDirExist($customDir); $imageUrl = explode('/', $imageUrl); $file = $imageUrl[count($imageUrl) - 1]; $filePath = $this->getDefaultSizeUploadDir() . DS . $file; if (!file_exists($customDir . DS . $file)) { $adapter = Mage::helper('Mage_Core_Helper_Data')->getImageAdapterType(); $image = new Varien_Image($filePath, $adapter); $widthOriginal = $image->getOriginalWidth(); $heightOriginal = $image->getOriginalHeight(); if ($width != $widthOriginal) { $widthOriginal = $width; } if ($height != $heightOriginal) { $heightOriginal = $height; } if ($widthOriginal != $image->getOriginalWidth() || $heightOriginal != $image->getOriginalHeight()) { $image->keepTransparency(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor(array(255, 255, 255)); $image->resize($widthOriginal, $heightOriginal); $image->save($customDir, basename($file)); } } return $this->getMediaUrl("custom/{$screenSize}/" . basename($file)); }
/** * Retrieve custom size image url * * @param string $imageFile * @param int $width * @param int $height * @return string|bool */ public function getCustomSizeImageUrl($imageFile, $width = 100, $height = 100) { /** @var $imageHelper Mage_XmlConnect_Helper_Image */ $imageHelper = Mage::helper('xmlconnect/image'); $screenSize = $width . 'x' . $height; $customDir = $imageHelper->getMediaPath('custom' . DS . $screenSize); $ioFile = new Varien_Io_File(); $ioFile->checkAndCreateFolder($customDir); $filePath = self::getBasePath() . DS . $imageFile; $isImagePng = true; if (!$ioFile->fileExists($filePath)) { return false; } $originalImageType = $this->_getImageType($filePath); if ($originalImageType !== IMAGETYPE_PNG) { $imageFile = $this->_convertFileExtensionToPng($imageFile); $isImagePng = false; } $customSizeFile = $customDir . DS . $imageFile; if (!file_exists($customSizeFile)) { if (!$isImagePng) { $filePath = $this->_forcedConvertPng($filePath, $customSizeFile, $originalImageType); } $image = new Varien_Image($filePath); $widthOriginal = $image->getOriginalWidth(); $heightOriginal = $image->getOriginalHeight(); if ($width != $widthOriginal) { $widthOriginal = $width; } if ($height != $heightOriginal) { $heightOriginal = $height; } if ($widthOriginal != $image->getOriginalWidth() || $heightOriginal != $image->getOriginalHeight()) { $image->keepTransparency(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor(array(0, 0, 0)); $image->resize($widthOriginal, $heightOriginal); $image->save($customDir, basename($imageFile)); } else { $ioFile->cp($filePath, $customSizeFile); } } return $imageHelper->getMediaUrl("custom/{$screenSize}/" . basename($imageFile)); }
public function _resizeImage($image, $config = array(), $type = "product", $folder = 'resized') { if ((int) $config['function'] == 0 || $config['width'] <= 0) { return $image; } $_file_name = substr(strrchr($image, "/"), 1); $_media_dir = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . 'catalog' . DS . $type . DS; $cache_dir = $_media_dir . $folder . DS . $config['width'] . 'x' . $config['height'] . DS . md5(serialize($config)); $dirImg = Mage::getBaseDir() . str_replace("/", DS, strstr($image, '/media')); $from_skin_nophoto = Mage::getBaseDir() . str_replace("/", DS, strstr($image, '/skin')); $dirImg = strpos($dirImg, 'media') !== false ? $dirImg : ''; $dirImg = strpos($from_skin_nophoto, 'skin') !== false && $dirImg == '' ? $from_skin_nophoto : $dirImg; $new_image = ''; if (file_exists($cache_dir . DS . $_file_name) && @getimagesize($cache_dir . DS . $_file_name) !== false) { $new_image = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/' . $type . '/' . $folder . '/' . $config['width'] . 'x' . $config['height'] . '/' . md5(serialize($config)) . '/' . $_file_name; } elseif (file_exists($dirImg) && $dirImg != '') { if (!is_dir($cache_dir)) { @mkdir($cache_dir, 0777); } $height = $config['height'] == '' ? null : $config['height']; $hex = $config['background']; $rgbColor = $this->HexToRGB($hex); $image = new Varien_Image($dirImg); $image->constrainOnly($config['constrainOnly']); $image->keepFrame($config['keepFrame']); $image->keepTransparency($config['keepTransparency']); $image->keepAspectRatio($config['keepAspectRatio']); $image->backgroundColor($rgbColor); $image->resize($config['width'], $height); $image->save($cache_dir . DS . $_file_name); $new_image = Mage::getBaseUrl('media') . 'catalog/' . $type . '/' . $folder . '/' . $config['width'] . 'x' . $config['height'] . '/' . md5(serialize($config)) . '/' . $_file_name; } else { return $image; } return $new_image; }
/** * Generate product thumbnails * @param unknown_type $product */ public function generateThumb($product, $debug = false) { $thumsize = Mage::helper('solrsearch')->getSetting('autocomplete_thumb_size'); $width = 32; $height = 32; if (!empty($thumsize)) { $thumbSizeArray = explode('x', $thumsize); if (isset($thumbSizeArray[0]) && is_numeric($thumbSizeArray[0])) { if (isset($thumbSizeArray[1]) && is_numeric($thumbSizeArray[1])) { $width = trim($thumbSizeArray[0]); $height = trim($thumbSizeArray[1]); } } } $productId = $product->getId(); $image = trim($product->getSmallImage()); if (empty($image) || $image == 'no_selection') { $image = trim($product->getImage()); } if (empty($image) || $image == 'no_selection') { $productImagePath = Mage::helper('solrsearch/image')->getImagePlaceHolder(); } else { $productImagePath = Mage::getBaseDir("media") . DS . 'catalog' . DS . 'product' . DS . $image; } if (!file_exists($productImagePath)) { if ($product->getImage() != 'no_selection' && $product->getImage()) { $productImagePath = Mage::helper('solrsearch/image')->init($product, 'image')->resize($width, $height)->getImagePath(); if (!file_exists($productImagePath)) { $productImagePath = Mage::helper('solrsearch/image')->getImagePlaceHolder(); } } } $productImageThumbPath = Mage::getBaseDir('media') . DS . "catalog" . DS . "product" . DS . "sb_thumb" . DS . $productId . '.jpg'; if (file_exists($productImageThumbPath)) { unlink($productImageThumbPath); } $imageResizedUrl = Mage::getBaseUrl("media") . DS . "catalog" . DS . "product" . DS . "sb_thumb" . DS . $productId . '.jpg'; try { $imageObj = new Varien_Image($productImagePath); $imageObj->constrainOnly(FALSE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(FALSE); $imageObj->backgroundColor(array(255, 255, 255)); //$imageObj->keepTransparency(TRUE); $imageObj->resize($width, $height); $imageObj->save($productImageThumbPath); } catch (Exception $e) { echo 'Exception at product[' . $productId . '][' . $productImageThumbPath . ']: ', $e->getMessage(), "\n"; } if (file_exists($productImageThumbPath)) { return true; } return false; }
/** * @param string $imagePath * @param int|null $width * @param int|null $height * @return string */ protected function _getImageUrl($imagePath, $width = null, $height = null) { $imagePath = trim($imagePath); $mediaDir = Mage::getBaseDir('media'); $cacheDir = implode('/', array('gallery', 'image', 'cache')); $path = $mediaDir . DS . $cacheDir; if (!is_dir($path)) { mkdir($path, 0777, true); } if (!$imagePath || !is_file($mediaDir . DS . $imagePath)) { $imagePath = $this->_getPlaceHolderPath(); } $baseFileName = $imagePath; $fileName = (int) $width . '-' . (int) $height . '-' . basename($baseFileName); $filePath = $path . DS . $fileName; if (!is_file($filePath)) { $imageAbsPath = $mediaDir . DS . $baseFileName; if (!is_file($imageAbsPath)) { $imageAbsPath = $this->_getPlaceHolderPath(); } $image = new Varien_Image($imageAbsPath); $image->keepAspectRatio(true); $image->keepFrame(true); $image->constrainOnly(true); $image->backgroundColor(array(0, 0, 0)); if ($width) { $image->resize(intval($width), $height); } $image->save($path, $fileName); } return Mage::getBaseUrl('media') . $cacheDir . '/' . $fileName; }
/** * Retrieve thumbnail image url * * @param int $width * @return string|null */ public function getCustomSizeImageUrl($imageUrl, $width = 100, $height = 100) { $customDirRoot = Mage::getBaseDir('media') . DS . 'xmlconnect' . DS . 'custom'; $screenSize = $width . 'x' . $height; $customDir = $customDirRoot . DS . $screenSize; $this->_verifyDirExist($customDir); $imageUrl = explode('/', $imageUrl); $file = $imageUrl[count($imageUrl) - 1]; $filePath = $this->getDefaultSizeUploadDir() . DS . $file; if (!file_exists($customDir . $file)) { $image = new Varien_Image($filePath); $widthOriginal = $image->getOriginalWidth(); $heightOriginal = $image->getOriginalHeight(); if ($width != $widthOriginal) { $widthOriginal = $width; } if ($height != $heightOriginal) { $heightOriginal = $height; } if ($widthOriginal != $image->getOriginalWidth() || $heightOriginal != $image->getOriginalHeight()) { $image->keepTransparency(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor(array(255, 255, 255)); $image->resize($widthOriginal, $heightOriginal); $image->save($customDir, basename($file)); } } return Mage::getBaseUrl('media') . "xmlconnect/custom/{$screenSize}/" . basename($file); }
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 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('*/*/'); }
/** * Create preview image * * @param string $imagePath * @return string */ public function createPreviewImage($imagePath) { $adapter = Mage::helper('Mage_Core_Helper_Data')->getImageAdapterType(); $image = new Varien_Image($imagePath, $adapter); $image->keepTransparency(true); $image->constrainOnly(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor(array(255, 255, 255)); $image->resize(self::PREVIEW_IMAGE_WIDTH, self::PREVIEW_IMAGE_HEIGHT); $imageName = uniqid('preview_image_') . image_type_to_extension($image->getMimeType()); $image->save($this->_getImagePathPreview(), $imageName); $this->setPreviewImage($imageName); return $imageName; }
public function uploadBrandImage($brandId, $imageFile) { $this->createImageFolder($brandId); $brandImagePath = $this->getImagePath($brandId); $brandImagePathCache = $this->getImagePathCache($brandId); $imageName = ""; $newImageName = ""; if (isset($imageFile['name']) && $imageFile['name'] != '') { try { /* Starting upload */ $imageName = $imageFile['name']; $uploader = new Varien_File_Uploader('image'); $newImageName = $this->refineImageName($imageName); // Any extention would work $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $uploader->save($brandImagePath, $newImageName); $newImageName = $uploader->getUploadedFileName(); $fileImg = new Varien_Image($brandImagePath . DS . $newImageName); $fileImg->keepAspectRatio(true); $fileImg->keepFrame(true); $fileImg->keepTransparency(true); $fileImg->constrainOnly(false); $fileImg->backgroundColor(array(255, 255, 255)); $fileImg->resize(60, 60); $fileImg->save($brandImagePathCache . DS . $newImageName, null); /* if($newImageName != $imageName){ copy($brandImagePath .DS. $imageName,$brandImagePath .DS.$newImageName); unlink($brandImagePath.DS.$imageName); } */ } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } $imageName = $newImageName; } return $imageName; }
/** * Retrieve custom size image url * * * @param string $imageUrl * @param int $width * @param int $height * @return string|null */ public function getCustomSizeImageUrl($imageUrl, $width = 100, $height = 100) { $screenSize = $width . 'x' . $height; $customDir = $this->getMediaPath('custom' . DS . $screenSize); $this->_verifyDirExist($customDir); $imageUrl = explode('/', $imageUrl); $file = array_pop($imageUrl); $filePath = Mage_XmlConnect_Model_Images::getBasePath() . DS . $file; if (!file_exists($customDir . DS . $file)) { $image = new Varien_Image($filePath); $widthOriginal = $image->getOriginalWidth(); $heightOriginal = $image->getOriginalHeight(); if ($width != $widthOriginal) { $widthOriginal = $width; } if ($height != $heightOriginal) { $heightOriginal = $height; } if ($widthOriginal != $image->getOriginalWidth() || $heightOriginal != $image->getOriginalHeight()) { $image->keepTransparency(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor(array(255, 255, 255)); $image->resize($widthOriginal, $heightOriginal); $image->save($customDir, basename($file)); } } return $this->getMediaUrl("custom/{$screenSize}/" . basename($file)); }
public function saveAction() { $store_id = $this->getRequest()->getParam('store', 0); //zend_debug::dump($store_id);die(); $request = $this->getRequest(); $giftwrap = Mage::getModel('giftwrap/giftwrap'); if ($id = (int) $request->getParam('id')) { $giftwrap->load($id); } try { $image = ''; if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') { try { $uploader = new Varien_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF', 'png', 'PNG')); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); $path = Mage::getBaseDir('media') . DS . 'giftwrap'; $uploader->save($path, $_FILES['image']['name']); } catch (Exception $e) { } $image = $_FILES['image']['name']; } elseif ($giftwrap->getImage() != '') { $image = $giftwrap->getImage(); } $post = $request->getPost(); if (isset($post['image']['delete']) && $post['image']['delete'] == 1) { $image = ''; } if (isset($image) && $image != '') { try { $path = Mage::getBaseDir('media') . DS . 'giftwrap'; $fileImg = new Varien_Image($path . DS . $image); $fileImg->keepAspectRatio(true); $fileImg->keepFrame(true); $fileImg->keepTransparency(true); $fileImg->constrainOnly(false); $fileImg->backgroundColor(array(255, 255, 255)); $fileImg->resize(200, 200); $fileImg->save($path . DS . $image, null); } catch (Exception $e) { } } if (!$this->getRequest()->getParam('id')) { $giftwrap->setTitle($request->getParam('title'))->setPrice($request->getParam('price'))->setImage($image)->setStatus($request->getParam('status'))->setCharacter($request->getParam('character'))->setPersonalMessage($request->getParam('personal_message'))->setSortOrder($request->getParam('sort_order'))->setStoreId(0); $giftwrap->save(); $giftwrap->load($giftwrap->getId())->setOptionId($giftwrap->getId())->save(); $optionId = $giftwrap->getId(); $id = $giftwrap->getId(); $store_id = 0; $giftwrap->setId(null); foreach (Mage::getModel('core/store')->getCollection() as $store) { $giftwrap->setTitle($request->getParam('title'))->setPrice($request->getParam('price'))->setImage($image)->setStatus($request->getParam('status'))->setCharacter($request->getParam('character'))->setPersonalMessage($request->getParam('personal_message'))->setSortOrder($request->getParam('sort_order'))->setStoreId($store->getId())->setOptionId($optionId); $giftwrap->save(); $giftwrap->setId(null); } } else { if ($store_id != 0) { $giftwrapModel = Mage::getModel('giftwrap/giftwrap')->load($this->getRequest()->getParam('id')); $giftwrapModel->setTitle($request->getParam('title'))->setPrice($request->getParam('price'))->setImage($image)->setStatus($request->getParam('status'))->setCharacter($request->getParam('character'))->setPersonalMessage($request->getParam('personal_message'))->setSortOrder($request->getParam('sort_order'))->setDefaultTitle($request->getParam('default_title'))->setDefaultPrice($request->getParam('default_price'))->setDefaultImage($request->getParam('default_image'))->setDefaultStatus($request->getParam('default_status'))->setDefaultCharacter($request->getParam('default_character'))->setDefaultPersonalMessage($request->getParam('default_personal_message'))->setDefaultSortOrder($request->getParam('default_sort_order')); $giftwrapModel->save(); $arrFielName = array(0 => 'title', 1 => 'price', 2 => 'character', 3 => 'image', 4 => 'personal_message', 5 => 'status', 6 => 'sort_order'); $giftwrapDefault = Mage::getModel('giftwrap/giftwrap')->getCollection()->addFieldToFilter('store_id', '0')->addFieldToFilter('option_id', $giftwrapModel->getOptionId())->getFirstItem(); $test = array(); $count = 0; foreach ($arrFielName as $fielname) { if ($giftwrapModel->getData('default_' . $fielname) == '1') { $test[] = $fielname; $count++; $giftwrapModel->setData($fielname, $giftwrapDefault->getData($fielname)); } } // var_dump($test);die(); $id = $giftwrapModel->getId(); if ($count > 0) { $giftwrapModel->save(); $giftwrapModel->setId(null); } } else { // all store = 0 $giftwrapModel = Mage::getModel('giftwrap/giftwrap')->load($this->getRequest()->getParam('id')); //zend_debug::dump($giftwrapModel);die(); $giftwrapModel->setTitle($request->getParam('title'))->setPrice($request->getParam('price'))->setImage($image)->setStatus($request->getParam('status'))->setCharacter($request->getParam('character'))->setPersonalMessage($request->getParam('personal_message'))->setSortOrder($request->getParam('sort_order'))->setDefaultTitle($request->getParam('default_title'))->setDafeultPrice($request->getParam('default_price'))->setDefaultImage($request->getParam('default_image'))->setDefaultStatus($request->getParam('default_status'))->setDefaultCharacter($request->getParam('default_character'))->setDefaultPersonalMessage($request->getParam('default_personal_message'))->setDefaultSortOrder($request->getParam('default_sort_order')); $giftwrapModel->save(); // start HoaNTT $giftwrapCollection = Mage::getSingleton('giftwrap/giftwrap')->getCollection(); foreach ($giftwrapCollection as $_giftwrap) { $_giftwrap->setStatus($request->getParam('status')); $_giftwrap->save(); } // end HoaNTT - Magestore $arrFielName = array(0 => 'title', 1 => 'price', 2 => 'character', 3 => 'image', 4 => 'personal_message', 5 => 'status', 6 => 'sort_order'); foreach (Mage::getModel('core/store')->getCollection() as $store) { $col = Mage::getModel('giftwrap/giftwrap')->getCollection()->addFieldToFilter('store_id', $store->getId())->addFieldToFilter('option_id', $giftwrapModel->getOptionId())->getFirstItem(); $giftwrapStore = Mage::getModel('giftwrap/giftwrap')->load($col->getId()); $count = 0; foreach ($arrFielName as $fielname) { if ($giftwrapStore->getData('default_' . $fielname) == '1') { $count++; $giftwrapStore->setData($fielname, $giftwrapModel->getData($fielname)); } } $id = $giftwrapStore->getId(); if ($count > 0) { $giftwrapStore->save(); $giftwrapStore->setId(null); } } } } if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $id, 'store' => $store_id)); return; } $this->_redirect('*/*'); } catch (Mage_Core_Exception $e) { $this->_getSession()->addError(nl2br($e->getMessage())); $this->_getSession()->setData('giftwrap_form_data', $this->getRequest()->getParams()); } catch (Exception $e) { $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('Error while saving this giftwrap style. Please try again later.')); $this->_getSession()->setData('giftwrap_form_data', $this->getRequest()->getParams()); } $this->_forward('new'); }