function getImage($src, $xSize = 150, $ySize = 150, $keepRatio = true, $styles = "") { if ($src != "") { $image = new Varien_Image(Mage::getBaseDir('media') . DS . $src); $image->constrainOnly(false); $image->keepAspectRatio($keepRatio); $image->setImageBackgroundColor(0xffffff); $image->keepTransparency(true); $image->resize($xSize, $ySize); $image->save(Mage::getBaseDir('media') . DS . 'stores/cache/' . basename($src)); return "<img style='" . $styles . "' src='" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'stores/cache/' . basename($src) . "'/>"; } else { return; } }
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; }
/** * @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); }