Created
March 11, 2013 13:49
-
-
Save ppassmannpriv/5134353 to your computer and use it in GitHub Desktop.
Yay fun Resize an image wherever you want with this fun and nice dandy helper in Magento.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Pieter Paßmann @ graphodata AG 2013 | |
* @website graphodata.de | |
* @description Resize any image with your own helper. | |
*/ | |
class Graphodata_Resizeimg_Helper_Data extends Mage_Core_Helper_Abstract | |
{ | |
/** | |
* 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 | |
*/ | |
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=MAGENTO_ROOT.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=MAGENTO_ROOT.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); | |
$imageObj->keepAspectRatio(TRUE); | |
$imageObj->resize($width,$height); | |
$imageObj->save($imageResizedPath); | |
endif; | |
/** | |
* 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 '/'.$imgUrl."/".$resizeFolder."/".$imgName; | |
} | |
/** | |
* Splits images Path and Name | |
* | |
* Path=custom/module/images/ | |
* Name=example.jpg | |
* | |
* @param string $imageValue | |
* @param string $attr | |
* @return string | |
*/ | |
public function splitImageValue($imageValue,$attr="name"){ | |
$imArray=explode("/",$imageValue); | |
$name=$imArray[count($imArray)-1]; | |
$path=implode("/",array_diff($imArray,array($name))); | |
if($attr=="path"){ | |
return $path; | |
} | |
else | |
return $name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment