Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:59:02+00:00 2026-06-10T22:59:02+00:00

I have a form with upload image field. How to resize the recently uploaded

  • 0

I have a form with upload image field. How to resize the recently uploaded image to desired size?
My current form is :

<?php
class Application_Form_User extends Zend_Form
{
public function init()
{
    $this->setAttrib('enctype', 'multipart/form-data');
    $this->setAction("");
    $this->setMethod("post");

    $element = new Zend_Form_Element_File('photo');
    $element->setLabel('Upload an image:')
            ->setValueDisabled(true);
    $this->addElement($element, 'photo');
    $element->addValidator('Count', false, 1);
    // limit to 1000K
    $element->addValidator('Size', false, 1024000);
    // only JPEG, PNG, and GIFs
    $element->addValidator('Extension', false, 'jpg,png,gif');
    $submit = $this->createElement('submit', 'submit');
    $submit->setLabel('Save');
    $this->addElement($submit);
}

}

And my controller:

public function indexAction()
{
    $form=new Application_Form_User();
    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $file=pathinfo($form->photo->getFileName());
            $form->photo->addFilter('Rename', PUBLIC_PATH.'/images/'.uniqid().time().'.'.$file['extension']);
            if ($form->photo->receive()) {
                $this->view->photo=pathinfo($form->photo->getFileName());
            }
        }
    }
    $this->view->form=$form;
}

Can somebody provide me with an example? How can i use plugins like php thumbnailer or similar plugin to resize the uploaded image?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:59:03+00:00Added an answer on June 10, 2026 at 10:59 pm

    Skoch_Filter_File_Resize works. It is a custom filter: http://eliteinformatiker.de/2011/09/02/thumbnails-upload-and-resize-images-with-zend_form_element_file/

    <?php
    // Skoch/Filter/File/Resize.php
    /**
     * Zend Framework addition by skoch
     * 
     * @category   Skoch
     * @package    Skoch_Filter
     * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
     * @author     Stefan Koch <cct@stefan-koch.name>
     */
    
    /**
     * @see Zend_Filter_Interface
     */
    require_once 'Zend/Filter/Interface.php';
    
    /**
     * Resizes a given file and saves the created file
     *
     * @category   Skoch
     * @package    Skoch_Filter
     */
    class Skoch_Filter_File_Resize implements Zend_Filter_Interface
    {
        protected $_width = null;
        protected $_height = null;
        protected $_keepRatio = true;
        protected $_keepSmaller = true;
        protected $_directory = null;
        protected $_adapter = 'Skoch_Filter_File_Resize_Adapter_Gd';
    
        /**
         * Create a new resize filter with the given options
         *
         * @param Zend_Config|array $options Some options. You may specify: width, 
         * height, keepRatio, keepSmaller (do not resize image if it is smaller than
         * expected), directory (save thumbnail to another directory),
         * adapter (the name or an instance of the desired adapter)
         * @return Skoch_Filter_File_Resize An instance of this filter
         */
        public function __construct($options = array())
        {
            if ($options instanceof Zend_Config) {
                $options = $options->toArray();
            } elseif (!is_array($options)) {
                require_once 'Zend/Filter/Exception.php';
                throw new Zend_Filter_Exception('Invalid options argument provided to filter');
            }
    
            if (!isset($options['width']) && !isset($options['height'])) {
                require_once 'Zend/Filter/Exception.php';
                throw new Zend_Filter_Exception('At least one of width or height must be defined');
            }
    
            if (isset($options['width'])) {
                $this->_width = $options['width'];
            }
            if (isset($options['height'])) {
                $this->_height = $options['height'];
            }
            if (isset($options['keepRatio'])) {
                $this->_keepRatio = $options['keepRatio'];
            }
            if (isset($options['keepSmaller'])) {
                $this->_keepSmaller = $options['keepSmaller'];
            }
            if (isset($options['directory'])) {
                $this->_directory = $options['directory'];
            }
            if (isset($options['adapter'])) {
                if ($options['adapter'] instanceof Skoch_Filter_File_Resize_Adapter_Abstract) {
                    $this->_adapter = $options['adapter'];
                } else {
                    $name = $options['adapter'];
                    if (substr($name, 0, 33) != 'Skoch_Filter_File_Resize_Adapter_') {
                        $name = 'Skoch_Filter_File_Resize_Adapter_' . ucfirst(strtolower($name));
                    }
                    $this->_adapter = $name;
                }
            }
    
            $this->_prepareAdapter();
        }
    
        /**
         * Instantiate the adapter if it is not already an instance
         *
         * @return void
         */
        protected function _prepareAdapter()
        {
            if ($this->_adapter instanceof Skoch_Filter_File_Resize_Adapter_Abstract) {
                return;
            } else {
                $this->_adapter = new $this->_adapter();
            }
        }
    
        /**
         * Defined by Zend_Filter_Interface
         *
         * Resizes the file $value according to the defined settings
         *
         * @param  string $value Full path of file to change
         * @return string The filename which has been set, or false when there were errors
         */
        public function filter($value)
        {
            if ($this->_directory) {
                $target = $this->_directory . '/' . basename($value);
            } else {
                $target = $value;
            }
    
            return $this->_adapter->resize($this->_width, $this->_height,
                $this->_keepRatio, $value, $target, $this->_keepSmaller);
        }
    }
    

    <?php
    // Skoch/Filter/File/Resize/Adapter/Abstract.php
    /**
     * Zend Framework addition by skoch
     * 
     * @category   Skoch
     * @package    Skoch_Filter
     * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
     * @author     Stefan Koch <cct@stefan-koch.name>
     */
    
    
    /**
     * Resizes a given file and saves the created file
     *
     * @category   Skoch
     * @package    Skoch_Filter
     */
    abstract class Skoch_Filter_File_Resize_Adapter_Abstract
    {
        abstract public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true);
    
        protected function _calculateWidth($oldWidth, $oldHeight, $width, $height)
        {
            // now we need the resize factor
            // use the bigger one of both and apply them on both
            $factor = max(($oldWidth/$width), ($oldHeight/$height));
            return array($oldWidth/$factor, $oldHeight/$factor);
        }
    }
    

    <?php
    // Skoch/Filter/File/Resize/Adapter/Gd.php
    /**
     * Zend Framework addition by skoch
     * 
     * @category   Skoch
     * @package    Skoch_Filter
     * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
     * @author     Stefan Koch <cct@stefan-koch.name>
     */
    
    require_once 'Skoch/Filter/File/Resize/Adapter/Abstract.php';
    
    /**
     * Resizes a given file with the gd adapter and saves the created file
     *
     * @category   Skoch
     * @package    Skoch_Filter
     */
    class Skoch_Filter_File_Resize_Adapter_Gd extends
        Skoch_Filter_File_Resize_Adapter_Abstract
    {
        public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true)
        {
            list($oldWidth, $oldHeight, $type) = getimagesize($file);
    
            switch ($type) {
                case IMAGETYPE_PNG:
                    $source = imagecreatefrompng($file);
                    break;
                case IMAGETYPE_JPEG:
                    $source = imagecreatefromjpeg($file);
                    break;
                case IMAGETYPE_GIF:
                    $source = imagecreatefromgif($file);
                    break;
            }
    
            if (!$keepSmaller || $oldWidth > $width || $oldHeight > $height) {
                if ($keepRatio) {
                    list($width, $height) = $this->_calculateWidth($oldWidth, $oldHeight, $width, $height);
                }
            } else {
                $width = $oldWidth;
                $height = $oldHeight;
            }
    
            $thumb = imagecreatetruecolor($width, $height);
    
            imagealphablending($thumb, false);
            imagesavealpha($thumb, true);
    
            imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight);
    
            switch ($type) {
                case IMAGETYPE_PNG:
                    imagepng($thumb, $target);
                    break;
                case IMAGETYPE_JPEG:
                    imagejpeg($thumb, $target);
                    break;
                case IMAGETYPE_GIF:
                    imagegif($thumb, $target);
                    break;
            }
            return $target;
        }
    }
    

    $photo->addFilter(new Skoch_Filter_File_Resize(array(
        'width' => 200,
        'height' => 300,
        'keepRatio' => true,
    )));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coding a form using PHP and jQuery. I have a file upload field
Background : I have a simple model form with a image field. class XYZForm(ModelForm):
I have this Form I am trying to upload my image but every time
I have an upload form with a file to be uploaded. The issue I
I have a upload field where people can upload a image. I'd like to
I have an html input field, such as <form method=post action=process.php enctype=multipart/form-data> <div> <h3>Files:</h3>
I have an image upload form that takes a title and a file for
Currently I have a model 'Locations' that has an image upload field added to
I have a form to upload images to my website. The consists on a
I have an upload form that takes a user about 30 min. to complete.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.