Magento – How to resize any images in Magento

Say we have an image located in the media folder /media/example.jpg
First, we need to initialize the $folderURL variable:

1
$folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

Then, the image URL:

1
$imageURL = $folderURL . $fileName;

Note: The format for $fileName = can be: ‘example.jpg’; or ‘testfolder/example.jpg’;
Next, we need to specify the full path for the existing file name ($basePath) and the name of the file that needs to be resized ($newPath). In this case they are:

1
2
$basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $fileName;
$newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "resized" . DS . $fileName;

After this, we need to create an instance of Varien_Image class

1
2
3
4
5
6
7
8
9
//$basePath - origin file location
$imageObj = new Varien_Image($basePath);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(FALSE);
$imageObj->keepFrame(FALSE);
//$width, $height - sizes you need (Note: when keepAspectRatio(TRUE), height would be ignored)
$imageObj->resize($width, $height);
//$newPath - name of resized image
$imageObj->save($newPath);

Then, we need to get the URL of the resized image:

1
$resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName;

Lastly, let’s make the function resizeImg($fileName, $width, $height = ”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public function resizeImg($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) . DS . $fileName;
    $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "resized" . DS . $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(TRUE);
            $imageObj->keepAspectRatio(FALSE);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize($width, $height);
            $imageObj->save($newPath);
        }
        $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName;
     } else {
        $resizedURL = $imageURL;
     }
     return $resizedURL;
}

That’s it! I hope this article helps you. Until next time, Happy Coding!

Advertisements

magento how to add custom image thumbnail in edit form

===================== in app\code\local\Nikunj\Contact\Block\Adminhtml\Contact\Edit\Tab\Form.php

For a specific case where the need to add a specific image, the logical way is to use the input_type “image” like this:

1
2
3
4
5
$fieldset->addField('imagename', 'image', array(
    'label'     => Mage::helper('customerprojects')->__('Thumbnail'),
    'name'      => 'imagename',
));

And the result is the following form:

image showing the icon but with an input field with a browse button and a delete checkbox=======================================================================

if u not store full path in data base then set this code =>

 if($form->getElement(‘imagename‘)->getValue() != ”)
{
$p = $form->getElement(‘imagename‘)->getValue();
$form->getElement(‘filename’)->setValue(‘contact/resized/’ . $p);
}

enjoy 🙂

Magento – how to resized image in magento

///////////////////// image resize start ///////////////
$imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).”contact/”.$imageFile;
$imageName = substr(strrchr($imageUrl,”/”),1);
$imageResized = Mage::getBaseDir(‘media’).DS.”contact”.DS.”resized”.DS.$imageName;
// changing image url into direct path
$dirImg = Mage::getBaseDir().str_replace(“/”,DS,strstr($imageUrl,’/media’));
// if resized image doesn’t exist, save the resized image to the resized directory
if(!file_exists($imageResized) && file_exists($dirImg)) :
$imageObj = new Varien_Image($dirImg);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(FALSE);
$imageObj->keepFrame(FALSE);
$imageObj->resize(120, 120);
$imageObj->save($imageResized);
endif;
///////////////////// image resize enf ///////////////