Sometimes it is necessary to know the real size of an image before rendering and displaying it on a page. Below you can see the function which returns the array of sizes (such as heigh and width) for a specific image.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Define real sizes of image from image folder * * @param string $filename * @return array */ public function getImgRealSizes($filename) { $path = Mage::getBaseDir('media') . DS . $this->getMediaDir() . DS . $filename; if (file_exists($path)) { $imageObj = new Varien_Image($path); return array('height' => $imageObj->getOriginalHeight(), 'width' => $imageObj->getOriginalWidth()); } return FALSE; } |
Here: $path = Mage::getBaseDir(‘media’) . DS . $this->getMediaDir() . DS . $filename; – full path to the image file
This function is suitable for such modules as Animated Slider and Splashpage.
Here is an example of its usage:
1 2 3 4 5 6 7 8 9 10 11 12 |
public function getMediaDir($filename) { return ‘myfolder’; } public function myFunction() { $filename = ‘mypicture.jpg’; $sizes = $this->getImgRealSizes($filename); echo $sizes[‘height’] /* display real height of current image */ echo $sizes[‘width’] /* display rela width */ } |
Partner With Us
Looking for a partner to grow your business? We are the right company to bring your webstore to success.
Talk to Igor