0

I create a custom module in Magento 1.9 to upload some images. But I want to have this big images as a thumbnails too, how I can made a resize of the big images?

    if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != null) {
        $result['file'] = '';
        try {   
            $uploader = new Varien_File_Uploader('image');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(false);
            $path = Mage::getBaseDir('media') . DS.'images/'.DS ;
            $result = $uploader->save($path, $_FILES['image']['name'] );
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage() . '  '. $path);
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
        $data['image'] = 'images/'.$result['file'];
    }

How I can duplicate and resize uploaded images to create thumbnails automatically.

Thank you

2
  • do you want to resize image after saving images ?
    – Pawan
    Commented Mar 11, 2018 at 17:12
  • Hi, the images are already save, I just want to have a new variable for example small_images and this variable to be resized. The idea is to have thumbnails
    – Robert
    Commented Mar 11, 2018 at 17:53

2 Answers 2

1

Please check this link http://vagento.com/magento-resize-image-function/ I hope it'll help you with what you want.

4
  • but how I can duplicate the uploaded image?
    – Robert
    Commented Mar 12, 2018 at 13:01
  • @Robert Read the instruction on the provided link.
    – user53816
    Commented Mar 12, 2018 at 13:09
  • I already read that but there you don't duplicate nothing, there you just get the base image and then you resize the base image, I need to create the thumbnails automatically
    – Robert
    Commented Mar 12, 2018 at 13:14
  • will be fine if you can reproduce that code here with my requirements, thanks
    – Robert
    Commented Mar 12, 2018 at 13:30
1

For resize the image please use the below code. I hope it will work

if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != null) {
    $result['file'] = '';
    try {
        // changing image url into direct path
        $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($_FILES['image']['name'],'/media'));

        // resized image path (media/IMAGE_NAME)
        $imageResized = Mage::getBaseDir('media').DS."resized".DS.$_FILES['image']['name'];
        $imageObj = new Varien_Image($dirImg);
        $imageObj->constrainOnly(TRUE);
        $imageObj->keepAspectRatio(TRUE);
        $imageObj->keepFrame(FALSE);
        $imageObj->resize(120, 120);
        $result = $imageObj->save($imageResized);

    } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage() . '  '. $path);
        Mage::getSingleton('adminhtml/session')->setFormData($data);
        $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
        return;
    }
    $data['image'] = 'images/'.$result['file'];
}
3
  • Hi thanks, and how I will made the difference between big images and the thumbnails? So there I upload the big / main images in the backend, and I need to create thumbnails for the main images, so the main images will remain the same, I need to resize thumbnails, not main Image
    – Robert
    Commented Mar 11, 2018 at 20:07
  • You can save your main image in another path directory before the resize image. Commented Mar 12, 2018 at 16:08
  • perfect but how I can do this?
    – Robert
    Commented Mar 12, 2018 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.