public function createPreview($name, $pathModule, $id, $width, $height) { if (isset($_FILES[$name]['name']) && $_FILES[$name]['name'] != null) { $path = Mage::getBaseDir('media') . DS . $pathModule . DS; $imageObj = new Varien_Image($path . '/' . $_FILES[$name]['name']); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepFrame(FALSE); $currentRatio = $imageObj->getOriginalWidth() / $imageObj->getOriginalHeight(); $targetRatio = $width / $height; if ($targetRatio > $currentRatio) { $imageObj->resize($width, null); } else { $imageObj->resize(null, $height); } $diffWidth = $imageObj->getOriginalWidth() - $width; $diffHeight = $imageObj->getOriginalHeight() - $height; //$imageObj->resize($width, $height); $imageObj->crop(floor($diffHeight * 0.5), floor($diffWidth / 2), ceil($diffWidth / 2), ceil($diffHeight * 0.5)); if (empty($id)) { $id = Mage::getModel('evoqueflex/evoqueflex')->getCollection()->addOrder('slide_id', 'ASC')->getLastItem()->toArray(); $id = $id['slide_id']; } $userfile_extn = explode(".", strtolower($_FILES[$name]['name'])); $imageObj->save($path . 'preview_' . $id . '.' . $userfile_extn[1]); } }
public function getShortImageSize($item) { $width_max = Mage::getStoreConfig('clnews/news/shortdescr_image_max_width'); $height_max = Mage::getStoreConfig('clnews/news/shortdescr_image_max_height'); if (Mage::getStoreConfig('clnews/news/resize_to_max') == 1) { $width = $width_max; $height = $height_max; } else { $imageObj = new Varien_Image(Mage::getBaseDir('media') . DS . $item->getImageShortContent()); $original_width = $imageObj->getOriginalWidth(); $original_height = $imageObj->getOriginalHeight(); if ($original_width > $width_max) { $width = $width_max; } else { $width = $original_width; } if ($original_height > $height_max) { $height = $height_max; } else { $height = $original_height; } } if ($item->getShortWidthResize()) { $width = $item->getShortWidthResize(); } else { $width; } if ($item->getShortHeightResize()) { $height = $item->getShortHeightResize(); } else { $height; } return array('width' => $width, 'height' => $height); }
public function postAction() { if (!empty($_FILES)) { $type = 'file'; if (isset($_FILES[$type]['name']) && $_FILES[$type]['name'] != '') { try { $uploadsDir = Mage::getBaseDir('upload'); $uploader = new Varien_File_Uploader($type); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $path = Mage::getBaseDir('media') . DS . 'upload'; if (!is_dir($path)) { mkdir($path, 0777, true); } $uploader->save($path, $_FILES[$type]['name']); $filename = $uploader->getUploadedFileName(); $md5 = md5($filename); $owner = Mage::getSingleton('customer/session')->getCustomerId() ? Mage::getSingleton('customer/session')->getCustomerId() : Mage::getSingleton('customer/session')->getSessionId(); Mage::getModel('xxx_catalog/upload')->setMd5($md5)->setFilename($filename)->setOwner($owner)->save(); $varienImage = new Varien_Image($uploadsDir . $filename); $width = $varienImage->getOriginalWidth(); $height = $varienImage->getOriginalHeight(); $data = ['id' => $md5, 'width' => $width, 'height' => $height]; echo json_encode($data); die; } catch (Exception $e) { Mage::log($e->getMessage(), null, $this->_logFile); echo json_encode(['error' => $this->__($e->getMessage())]); } } } $this->getResponse()->setRedirect(Mage::getUrl('*/*/index')); }
public function getPopupActive() { $preview = $this->getRequest()->getParam('popup'); $popupID = $this->getRequest()->getParam('popupid'); if ($preview == "preview" && !empty($popupID)) { $arrPopup = Mage::getModel('popup/popup')->getPopupPreview($popupID); } else { $arrPopup = Mage::getModel('popup/popup')->getPopupActive(); } if (!empty($arrPopup)) { // If there is a link, process template tag if (!empty($arrPopup["popup_url"])) { $helper = Mage::helper('cms'); $processor = $helper->getPageTemplateProcessor(); $arrPopup["popup_url"] = $processor->filter($arrPopup["popup_url"]); } // If there is a content, process template tag if (!empty($arrPopup["popup_content_html"])) { $helper = Mage::helper('cms'); $processor = $helper->getPageTemplateProcessor(); $arrPopup["popup_content_html"] = $processor->filter($arrPopup["popup_content_html"]); } $popupID = $arrPopup["popup_id"]; $storeID = Mage::app()->getStore()->getId(); //Check Cookie $cookiePopup = Mage::getSingleton('core/cookie')->get('cookie_popup_' . $storeID); // If there's any cookie or new promo or preview mode if (!$cookiePopup || $cookiePopup != $popupID || $this->getRequest()->getParam('popup') == 'preview') { if (!$this->getRequest()->getParam('popup')) { Mage::getSingleton('core/cookie')->set('cookie_popup_' . $storeID, $popupID, 3600 * 24 * 7); } if (!empty($arrPopup["popup_image"])) { $pathImage = Mage::getBaseDir('media') . "/popup/" . $arrPopup["popup_image"]; if (file_exists($pathImage)) { $imageObj = new Varien_Image($pathImage); $arrPopup['popup_image_width'] = $imageObj->getOriginalWidth(); $arrPopup['popup_image_height'] = $imageObj->getOriginalHeight(); } } return $arrPopup; } } else { return false; } }
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 getImageSize($image = null, $_maxW = 125, $_maxH = 125, $fix = false) { $_baseSrc = Mage::getSingleton('igallery/config')->getBaseMediaPath(); if (file_exists($_baseSrc . $image->getFile())) { $_imageObject = new Varien_Image($_baseSrc . $image->getFile()); $_sizeArray = array($_imageObject->getOriginalWidth(), $_imageObject->getOriginalHeight()); $_defaultW = $_maxW; $_defaultH = $_maxH; if ($_sizeArray[0] / $_sizeArray[1] > $_defaultW / $_defaultH) { $_defaultW *= floatval($_sizeArray[0] / $_sizeArray[1]) / floatval($_defaultW / $_defaultH); } else { $_defaultH *= floatval($_defaultW / $_defaultH) / floatval($_sizeArray[0] / $_sizeArray[1]); } if ($fix == 'width') { if ($_defaultW > $_maxW) { $_defaultH *= $_maxW / $_defaultW; $_defaultW = $_maxW; } } elseif ($fix == 'height') { if ($_defaultH > $_maxH) { $_defaultW *= $_maxH / $_defaultH; $_defaultH = $_maxH; } } else { if ($_defaultW > $_maxW) { $_defaultH *= $_maxW / $_defaultW; $_defaultW = $_maxW; } elseif ($_defaultH > $_maxH) { $_defaultW *= $_maxH / $_defaultH; $_defaultH = $_maxH; } } return new Varien_Object(array('width' => round($_defaultW), 'height' => round($_defaultH))); } return false; }
public function getImageSize($image = null, $w, $h, $fix = false) { if (file_exists($image->getPath())) { try { $i = new Varien_Image($image->getPath()); $s = array($i->getOriginalWidth(), $i->getOriginalHeight()); $_defaultW = $w; $_defaultH = $h; if ($s[0] / $s[1] > $_defaultW / $_defaultH) { $_defaultW *= floatval($s[0] / $s[1]) / floatval($_defaultW / $_defaultH); } else { $_defaultH *= floatval($_defaultW / $_defaultH) / floatval($s[0] / $s[1]); } if ($fix == 'width') { if ($_defaultW > $w) { $_defaultH *= $w / $_defaultW; $_defaultW = $w; } } elseif ($fix == 'height') { if ($_defaultH > $h) { $_defaultW *= $h / $_defaultH; $_defaultH = $h; } } else { if ($_defaultW > $w) { $_defaultH *= $w / $_defaultW; $_defaultW = $w; } elseif ($_defaultH > $h) { $_defaultW *= $h / $_defaultH; $_defaultH = $h; } } return new Varien_Object(array('width' => round($_defaultW), 'height' => round($_defaultH))); } catch (Exception $e) { } } return new Varien_Object(array('width' => round($w), 'height' => round($h))); }
/** * Returns array('success'=>true) or array('error'=>'error message') */ function handleUpload($uploadDirectory, $slider_dimensions, $replaceOldFile = FALSE) { if (!is_writable($uploadDirectory)) { return array('error' => Mage::helper('lookbookslider')->__("File can't be uploaded. Upload directory isn't writable.")); } if (!$this->filemodel) { return array('error' => Mage::helper('lookbookslider')->__("Uploader error. File was not uploaded.")); } $size = $this->filemodel->getSize(); if ($size == 0) { return array('error' => Mage::helper('lookbookslider')->__("File is empty")); } if ($size > $this->sizeLimit) { return array('error' => Mage::helper('lookbookslider')->__("File is too large")); } $pathinfo = pathinfo($this->filemodel->getName()); $filename = $pathinfo['filename']; //$filename = md5(uniqid()); $filename = uniqid(); $ext = $pathinfo['extension']; if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) { $these = implode(', ', $this->allowedExtensions); return array('error' => Mage::helper('lookbookslider')->__("File can't be uploaded. It has an invalid extension, it should be one of %s.", $these)); } if (!$replaceOldFile) { /// don't overwrite previous files that were uploaded while (file_exists($uploadDirectory . $filename . '.' . $ext)) { $filename .= rand(10, 99); } } if ($this->filemodel->save($uploadDirectory . $filename . '.' . $ext)) { $imgPathFull = $uploadDirectory . $filename . '.' . $ext; $image_dimensions = Mage::helper('lookbookslider')->getImageDimensions($imgPathFull); if (!isset($image_dimensions['error'])) { ///////////////////////////////////////////// $resized_image = new Varien_Image($imgPathFull); $resized_image->constrainOnly(FALSE); $resized_image->keepAspectRatio(TRUE); $resized_image->keepTransparency(TRUE); $resized_image->keepFrame(FALSE); if ($slider_dimensions['width'] / $slider_dimensions['height'] > $resized_image->getOriginalWidth() / $resized_image->getOriginalHeight()) { $resized_image->resize($slider_dimensions['width'], null); } else { $resized_image->resize(null, $slider_dimensions['height']); } $cropX = 0; $cropY = 0; if ($resized_image->getOriginalWidth() > $slider_dimensions['width']) { $cropX = intval(($resized_image->getOriginalWidth() - $slider_dimensions['width']) / 2); } elseif ($resized_image->getOriginalHeight() > $slider_dimensions['height']) { $cropY = intval(($resized_image->getOriginalHeight() - $slider_dimensions['height']) / 2); } $resized_image->crop($cropY, $cropX, $cropX, $cropY); $resized_image->save($imgPathFull); $image_dimensions = Mage::helper('lookbookslider')->getImageDimensions($imgPathFull); ///////////////////////////////////////////// } else { return array('error' => Mage::helper('lookbookslider')->__("Could not get uploaded image dimensions.")); } return array('success' => true, 'filename' => $filename . '.' . $ext, 'dimensions' => $image_dimensions); } else { return array('error' => Mage::helper('lookbookslider')->__("Could not save uploaded file. The upload was cancelled, or server error encountered")); } }
/** * 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)); }
/** * 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)); }
/** * 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)); }
public function getThumbnail($field_id, $filename, $width, $height = null) { $filename = Varien_File_Uploader::getCorrectFileName($filename); $imageUrl = $this->getFileFullPath($field_id, $filename); $file_info = @getimagesize($imageUrl); if (!$file_info) { return false; } if (strstr($file_info["mime"], "bmp")) { return false; } if (file_exists($imageUrl)) { $imageObj = new Varien_Image($imageUrl); } if (!$height && (double) substr(Mage::getVersion(), 0, 3) > 1) { $height = round($imageObj->getOriginalHeight() * ($width / $imageObj->getOriginalWidth())); } $imageResized = $this->getFilePath($field_id) . "thumb" . DS . $width . 'x' . $height . '_' . $filename; if (!file_exists($imageResized) && file_exists($imageUrl) || Mage::getStoreConfig('webforms/images/cache') == 0) { if ((double) substr(Mage::getVersion(), 0, 3) > 1) { $imageObj->keepAspectRatio(true); $imageObj->keepTransparency(true); } $imageObj->resize($width, $height); $imageObj->save($imageResized); } $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "webforms/" . $this->getId() . "/" . $field_id; if ($this->getData('key_' . $field_id)) { $url .= "/" . $this->getData('key_' . $field_id); } $url .= "/thumb/" . $width . 'x' . $height . '_' . urlencode($filename); return $url; }
public function resizeThumbnail1($thumbImage, $pathName, $width, $height) { $imagePathInput = Mage::getBaseDir('media') . DS . $pathName . DS . $thumbImage; $imageName = $thumbImage; if ($imageName == '') { $imageName = 'no-image.png'; $imagePathInput = Mage::getBaseDir('media') . DS . 'brands' . DS . 'no-image.png'; } $imagePathOutput = $pathName . DS . $width . 'x' . $height . DS . $imageName; $imageUrl = str_replace(DS, '/', $imagePathOutput); $imageUrl = Mage::getBaseUrl('media') . $imageUrl; $pathBaseDir = Mage::getBaseDir('media') . DS . $imagePathOutput; $this->setPlaceholder($imageUrl); if (!is_file($pathBaseDir)) { $_backgroundColor = array(255, 255, 255); try { $imageObj = new Varien_Image($imagePathInput); $imageObj->backgroundColor($_backgroundColor); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepFrame(TRUE); $h = $imageObj->getOriginalHeight(); $w = $imageObj->getOriginalWidth(); if ((double) $w / $h <= (double) $width / $height) { $height = $height; $width = $w / $h * $height; $imageObj->resize($width, $height); } else { $width = $width; $height = $h / $w * $width; $imageObj->resize($width, $height); } $imageObj->save($pathBaseDir); } catch (Exception $e) { } } return $this->getPlaceholder(); }
protected function addImage($url, $height = null, $weight = null) { $ext = array_reverse(explode(".", $url)); $ext = $ext[0]; $name = basename($url, "." . $ext); $tempFileName = Mage::getBaseDir('tmp') . "/{$name}" . time() . ".png"; switch ($ext) { case 'gif': imagepng(imagecreatefromgif($url), $tempFileName); break; case 'jpg': imagepng(imagecreatefromjpeg($url), $tempFileName); break; case 'png': file_put_contents($tempFileName, file_get_contents($url)); break; default: return 0; } $this->_tmpFiles[] = $tempFileName; $_image = new Varien_Image($tempFileName); $_image->keepAspectRatio(true); //$_image->keepFrame(true); $_image->keepTransparency(true); if ($height && $_image->getOriginalHeight() > $height) { $_image->resize(null, $height); } if ($weight && $_image->getOriginalWidth() > $weight) { $_image->resize($weight, null); } $_image->save($tempFileName); $img = Zend_Pdf_Image::imageWithPath($tempFileName); $this->_currentPage->drawImage($img, $this->getX(), $this->getY() - $_image->getOriginalHeight(), $this->getX() + $_image->getOriginalWidth(), $this->getY()); $this->setY($this->getY() - $_image->getOriginalHeight() - $this->getInterval()); }
public function cropResizeImg($fileName, $tmp, $width, $height = '') { $folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); $imageURL = $folderURL . $fileName; $basePath = $tmp; $newPath = Mage::getBaseDir() . DS . 'skin' . DS . 'adminhtml' . DS . 'base' . DS . 'default' . DS . 'evoque' . DS . 'images' . DS . 'Configsets' . DS . $fileName; if (is_file($tmp)) { $imageObj = new Varien_Image($basePath); $imageObj->constrainOnly(TRUE); $imageObj->keepAspectRatio(FALSE); $imageObj->keepFrame(FALSE); $currentRatio = $imageObj->getOriginalWidth() / $imageObj->getOriginalHeight(); $targetRatio = $width / $height; if ($targetRatio > $currentRatio) { $imageObj->resize($width, null); } else { $imageObj->resize(null, $height); } $diffWidth = $imageObj->getOriginalWidth() - $width; $diffHeight = $imageObj->getOriginalHeight() - $height; //$imageObj->resize($width, $height); $imageObj->crop(floor($diffHeight * 0.5), floor($diffWidth / 2), ceil($diffWidth / 2), ceil($diffHeight * 0.5)); $imageObj->save($newPath); } return $fileName; }
public function renderImageAttributes($relativeUrl, $width = null, $height = null) { if ($filename = $this->getFilename($relativeUrl, 'image')) { if ($width || $height) { $processor = new Varien_Image($filename); $newRelativeUrl = 'w' . ($width ? $width : 'x') . 'h' . ($height ? $height : 'x') . '/' . $relativeUrl; if (!$width) { $width = $processor->getOriginalWidth(); } if (!$height) { $height = $processor->getOriginalHeight(); } $processor->keepAspectRatio(true); $processor->resize($width, $height); $processor->save($this->getFilename($newRelativeUrl, 'image', true)); return "src=\"{$this->getUrl($newRelativeUrl, 'image')}\" " . "width=\"{$processor->getOriginalWidth()}\" " . "height=\"{$processor->getOriginalHeight()}\""; } else { return "src=\"{$this->getUrl($relativeUrl, 'image')}\""; } } }
public function init($object, $field) { if ($field) { $this->setField($field); } $image = $object->getData($this->field); if (!$image) { return false; } $this->getMedia(); // set media $Url = $this->getPath($object); $img = explode('/', $image); $img = end($img); $Url['original'] .= $img; $Url['resized'] .= $img; if (!is_file($Url['original'])) { return false; } if (file_exists($Url['resized'])) { $imageResizedObj = new Varien_Image($Url['resized']); if ($this->width != $imageResizedObj->getOriginalWidth() || $this->height != $imageResizedObj->getOriginalHeight() || filemtime($Url['original']) > filemtime($Url['resized'])) { $this->convertImage($Url['original'], $Url['resized']); } } else { if (file_exists($Url['original'])) { $this->convertImage($Url['original'], $Url['resized']); } } if (file_exists($Url['resized'])) { $Url['url_resized'] .= $img; return $Url['url_resized']; } else { $Url['url_original'] .= $img; return $Url['url_original']; } }
/** * save item action */ public function saveAction() { if (!Mage::helper('magenotification')->checkLicenseKeyAdminController($this)) { return; } if ($data = $this->getRequest()->getPost()) { if ($sourceFile = $this->_uploadAffiliateBannerFile('source_file')) { $data['source_file'] = $sourceFile; } // Peel large file uploading if ($peelImage = $this->_uploadAffiliateBannerFile('peel_image')) { $data['peel_image'] = $peelImage; } $bannerId = $this->getRequest()->getParam('id'); $storeId = $this->getRequest()->getParam('store'); $banner = Mage::getModel('affiliateplus/banner'); $banner->setStoreId($storeId)->load($bannerId)->addData($data)->setId($bannerId); // Prepare image size if (($sourceFile = $banner->getSourceFile()) && $banner->getTypeId() != Magestore_AffiliateplusBanner_Helper_Data::BANNER_TYPE_FLASH && (!$banner->getWidth() || !$banner->getHeight())) { try { $image = new Varien_Image(Mage::getBaseDir('media') . DS . 'affiliateplus' . DS . 'banner' . DS . $sourceFile); if (!$banner->getWidth()) { $banner->setWidth($image->getOriginalWidth()); } if (!$banner->getHeight()) { $banner->setHeight($image->getOriginalHeight()); } } catch (Exception $e) { Mage::logException($e); } } // Prepare image size for Peel banner if (($sourceFile = $banner->getPeelImage()) && $banner->getTypeId() == Magestore_AffiliateplusBanner_Helper_Data::BANNER_TYPE_PEEL && (!$banner->getPeelWidth() || !$banner->getPeelHeight())) { try { $image = new Varien_Image(Mage::getBaseDir('media') . DS . 'affiliateplus' . DS . 'banner' . DS . $sourceFile); if (!$banner->getPeelWidth()) { $banner->setPeelWidth($image->getOriginalWidth()); } if (!$banner->getPeelHeight()) { $banner->setPeelHeight($image->getOriginalHeight()); } } catch (Exception $e) { Mage::logException($e); } } // Try to save banner try { $banner->save(); if ($banner->getTypeId() == Magestore_AffiliateplusBanner_Helper_Data::BANNER_TYPE_ROTATOR && isset($data['banners'])) { $childBanners = array(); if ($banner->getData('banners')) { parse_str($banner->getData('banners'), $childBanners); } Mage::getSingleton('affiliateplusbanner/rotator')->setData('parent_id', $banner->getId())->saveChildBanner($childBanners); } Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('affiliateplusbanner')->__('Banner was successfully saved')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $banner->getId(), 'store' => $storeId)); return; } $this->_redirect('*/*/', array('store' => $storeId)); 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'), 'store' => $storeId)); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('affiliateplusbanner')->__('Unable to find banner to save')); $this->_redirect('*/*/', array('store' => $storeId)); }
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) { } }
/** * 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); }
/** * 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; }
/** * Splits images Path and Name * * img_path=lookbook/example.jpg * * @param string $img_path * @return array('width'=>$width, 'height'=>$height) */ public function getImageDimensions($img_path) { if (file_exists($img_path)) { $imageObj = new Varien_Image($img_path); $width = $imageObj->getOriginalWidth(); $height = $imageObj->getOriginalHeight(); $result = array('width' => $width, 'height' => $height); } else { $result = array('error' => "{$img_path} does not exists"); } return $result; }
public function addWatermarkIfNeed($imageLink) { if (!$this->isWatermarkEnabled()) { return $imageLink; } $imagePath = $this->imageLinkToPath($imageLink); if (!is_file($imagePath)) { return $imageLink; } $fileExtension = pathinfo($imagePath, PATHINFO_EXTENSION); $pathWithoutExtension = preg_replace('/\\.' . $fileExtension . '$/', '', $imagePath); $markingImagePath = $pathWithoutExtension . '-' . $this->getWatermarkHash() . '.' . $fileExtension; if (is_file($markingImagePath)) { $currentTime = Mage::helper('M2ePro')->getCurrentGmtDate(true); if (filemtime($markingImagePath) + self::WATERMARK_CACHE_TIME > $currentTime) { return $this->pathToImageLink($markingImagePath); } @unlink($markingImagePath); } $prevMarkingImagePath = $pathWithoutExtension . '-' . $this->getWatermarkPreviousHash() . '.' . $fileExtension; if (is_file($prevMarkingImagePath)) { @unlink($prevMarkingImagePath); } $varDir = new Ess_M2ePro_Model_General_VariablesDir(array('child_folder' => 'ebay/template/description/watermarks')); $watermarkPath = $varDir->getPath() . $this->getId() . '.png'; if (!is_file($watermarkPath)) { $varDir->create(); @file_put_contents($watermarkPath, $this->getWatermarkImage()); } $watermarkPositions = array(self::WATERMARK_POSITION_TOP => Varien_Image_Adapter_Abstract::POSITION_TOP_RIGHT, self::WATERMARK_POSITION_MIDDLE => Varien_Image_Adapter_Abstract::POSITION_CENTER, self::WATERMARK_POSITION_BOTTOM => Varien_Image_Adapter_Abstract::POSITION_BOTTOM_RIGHT); $image = new Varien_Image($imagePath); $imageOriginalHeight = $image->getOriginalHeight(); $imageOriginalWidth = $image->getOriginalWidth(); $image->open(); $image->setWatermarkPosition($watermarkPositions[$this->getWatermarkPosition()]); $watermark = new Varien_Image($watermarkPath); $watermarkOriginalHeight = $watermark->getOriginalHeight(); $watermarkOriginalWidth = $watermark->getOriginalWidth(); if ($this->isWatermarkScaleModeStretch()) { $image->setWatermarkPosition(Varien_Image_Adapter_Abstract::POSITION_STRETCH); } if ($this->isWatermarkScaleModeInWidth()) { $watermarkWidth = $imageOriginalWidth; $heightPercent = $watermarkOriginalWidth / $watermarkWidth; $watermarkHeight = (int) ($watermarkOriginalHeight / $heightPercent); $image->setWatermarkWidth($watermarkWidth); $image->setWatermarkHeigth($watermarkHeight); } if ($this->isWatermarkScaleModeNone()) { $image->setWatermarkWidth($watermarkOriginalWidth); $image->setWatermarkHeigth($watermarkOriginalHeight); if ($watermarkOriginalHeight > $imageOriginalHeight) { $image->setWatermarkHeigth($imageOriginalHeight); $widthPercent = $watermarkOriginalHeight / $imageOriginalHeight; $watermarkWidth = (int) ($watermarkOriginalWidth / $widthPercent); $image->setWatermarkWidth($watermarkWidth); } if ($watermarkOriginalWidth > $imageOriginalWidth) { $image->setWatermarkWidth($imageOriginalWidth); $heightPercent = $watermarkOriginalWidth / $imageOriginalWidth; $watermarkHeight = (int) ($watermarkOriginalHeight / $heightPercent); $image->setWatermarkHeigth($watermarkHeight); } } $opacity = 100; if ($this->isWatermarkTransparentEnabled()) { $opacity = 30; } $image->setWatermarkImageOpacity($opacity); $image->watermark($watermarkPath); $image->save($markingImagePath); return $this->pathToImageLink($markingImagePath); }
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; }