MEM
MEM

Reputation: 31337

Modus Operandi - Upload and Resize Multiple images using Zend Framework and HTML5

The good news:

The bad news:

The user must upload a file from any width or height - however no bigger then 8MB. The file must be stored on a specific folder (or database column). A thumbnail must be generated on a specific folder (or database column).

Those images must be associated with a specific record.

This is a "modus operandi" question, I realise that there is to much code involved here.

So:

  1. We first create our form element to support multiple upload, like this:

    $element = new Zend_Form_Element_File('multifile');
    $element->setAttrib('multiple', true);
    $element->setIsArray(true);
    
  2. We then, need to add some validations and allowed extensions;

  3. Then we need to process the upload;
  4. Once the upload is done, we need to resize those multiple files according to our needs.
  5. We need to store those resized files somewhere else.
  6. Once all this is done, we are ready to display the files associated with a specific database record?

Is this the way to go? Should I have more steps? Am I missing something. I've never done this before, but I'm taking it like a challenge.

Upvotes: 2

Views: 2968

Answers (1)

mingos
mingos

Reputation: 24502

First, you create a form. Not much complication here:

$this->addElement('File','Filedata');
$this->Filedata
    ->setLabel('Select images')
    ->setDestination('somepath') //define & create it somewhere earlier ;)
    ->addValidator('Size', false, 1024000)
    ->addValidator('Extension', false, 'jpg,png,gif')
    ->setMultiFile(5)
    ->addValidator('Count', false, array('min'=>0,'max' => 5))
;

In the controller, you receive the images. They will have temporary random names, which you can keep later if you wish (I usually do).

$fileinfo = $form->Filedata->getFileInfo();
$path = 'somepath'; //make sure it exists
for($i=0;$i<5;$i++) {
    if($fileinfo['Filedata_'.$i.'_']['tmp_name']) {
        $img = new Imagick($fileinfo['Filedata_'.$i.'_']['tmp_name']);
        // image processing goes here
        file_put_contents('somepath',$img);
    }
}

And that's it ;)

Upvotes: 4

Related Questions