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]); } }
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; }
/** * 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; }
/** * 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")); } }
/** * Returns the resized Image URL * * @param string $imgUrl - This is relative to the the media folder (custom/module/images/example.jpg) * @param int $x Width * @param int $y Height *Remember your base image or big image must be in Root/media/lookbookslider/example.jpg * * echo Mage::helper('lookbookslider')->getResizedUrl("lookbookslider/example.jpg",101,65) * *By doing this new image will be created in Root/media/lookbookslider/101X65/example.jpg */ 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 */ $width = $x; $y ? $height = $y : ($height = $x); /** * Resize folder is widthXheight */ $resizeFolder = $width . "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(FALSE); $imageObj->keepAspectRatio(TRUE); $imageObj->keepTransparency(TRUE); $imageObj->keepFrame(FALSE); // $imageObj->resize($width,$height); // $widthDistance = $imageObj->getOriginalWidth() - $width; // $heightDistance = $imageObj->getOriginalHeight() - $height; if ($width / $height > $imageObj->getOriginalWidth() / $imageObj->getOriginalHeight()) { $imageObj->resize($width, null); } else { $imageObj->resize(null, $height); } $cropX = 0; $cropY = 0; if ($imageObj->getOriginalWidth() > $width) { $cropX = intval(($imageObj->getOriginalWidth() - $width) / 2); } elseif ($imageObj->getOriginalHeight() > $height) { $cropY = intval(($imageObj->getOriginalHeight() - $height) / 2); } $imageObj->crop($cropY, $cropX, $cropX, $cropY); $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; }
/** * Get image after cropping it * @param string $content * @param int $width * @param int $height * @param string $ratio * @return string * @throws */ public function getCropImage($content, $width, $height, $ratio = '') { preg_match_all('/(?<!_)url=([\'"])?(.*?)\\1/', $content, $matches); if (isset($matches[2][0])) { $images = explode('/', $matches[2][0]); $sourceImage = Mage::getBaseDir('media') . DS . $matches[2][0]; $image = new Varien_Image($sourceImage); list($widthImg, $heightImg, , ) = getimagesize($sourceImage); if ($ratio == '') { $top = 0; $left = 0; $right = $widthImg - $width; $bottom = $heightImg - $height; } else { list($ratioWidth, $ratioHeight) = explode(':', $ratio); if ($heightImg * ($ratioWidth / $ratioHeight) > $widthImg) { $cropHeight = $widthImg / ($ratioWidth / $ratioHeight); $cropWidth = $widthImg; } else { $cropWidth = $heightImg * ($ratioWidth / $ratioHeight); $cropHeight = $heightImg; } $top = 0; $left = 0; $right = $widthImg - $cropWidth; $bottom = $heightImg - $cropHeight; } $image->crop($top, $left, $right, $bottom); $image->save(Mage::getBaseDir('media') . DS . 'wysiwyg' . DS . 'crop' . DS . $images[1]); return Mage::getBaseUrl('media') . '/wysiwyg/crop/' . $images[1]; } else { preg_match_all('/(?<!_)src=([\'"])?(.*?)\\1/', $content, $option); return $option[2][0]; } }
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; }