开发者

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

The good news:

  • I don't care if it uses ajax or not.
  • I don't care if the user must install a specific browser to make it work.
  • I don't care if there isn't any specifc progress bar.

The bad news:

  • I don't want to use flash.

T开发者_如何学运维he 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.


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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜