Magento Resize Image

Posted . Visible to the public.
    /**
     * Resize image recursively until it is less than 25KB and saved as tmp.jpg
     * Original image is unchanged     
     * 
     * @param string fullpath and filename
     * @return string fullpath tmp.jpg that is saved              
     */             
    protected function _resizeImage($fnm)
    {
        if (filesize($fnm) > 25600) {
          $image = new Varien_Image($fnm);
          $image->constrainOnly(false);         
          $image->keepAspectRatio(true);
          $newW = (int) $image->getOriginalWidth() * 0.95;
          $image->resize($newW);
          $fnm = Mage::getBaseDir('media').DS.'tmp'.DS.'tmp.jpg';
          $image->save($fnm); 
          $this->_resizeImage($fnm);         
        } 
        return $fnm;
    }
kiatng
Posted by kiatng to OpenMage (2014-03-07 02:04)