访问Zend上传文件

时间:2012-11-08 20:23:48

标签: php zend-framework image-processing

下面你会找到我的uploadAction函数。这段代码有效。我能够得到图像。但是,我想将自动调整大小机制集成到代码中。有人可以向我解释如何强制我在上传动作中上传文件,以便通过调整大小功能进行解析。

  public function uploadAction()
    {
        $request = $this->getRequest();

        $error = null;

        if ($request->isPost()) {
            if ($request->getParam('submit')) {

            // Create upload object
            $upload = new Zend_File_Transfer();

            // Add our validators
            $upload->addValidator('Size', false, 102400);
            $upload->addValidator('Extension', false, 'jpg,png,gif');

            // Set our destination
            $upload->setDestination(APPLICATION_PATH . '/../public/images/blog/');

                if(!$upload->receive()) {
                    $messages = $upload->getMessages();

                    foreach ($messages as $type => $message) {
                        switch($type) {
                            case 'fileExtensionFalse':
                                $this->view->error = 'File must be an image (jpg, gif, png)';
                                break;
                            case 'fileUploadErrorNoFile':
                                $this->view->error = 'Please select a file to upload';
                                break;
                            default:
                                $this->view->error = implode("/n", $messages);
                                break;
                        }
                    }
                }
                    print_r($messages); die();
            }
        }
    }

调整功能

 public function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 4)
    {
        if (empty($src_image) || empty($dst_image)) { return false; }
            if ($quality <= 1)
            {
                $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
                imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
                imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
                imagedestroy ($temp);
            }
            elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h))
            {
                $tmp_w = $dst_w * $quality;
                $tmp_h = $dst_h * $quality;
                $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
                imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
                imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
                imagedestroy ($temp);
            } else
            {
                imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
            }
            return true;
    }

我希望能够在上传时更改图像的大小和质量。访问我刚刚上传的文件的功能或调用是什么,或者我可以在保存之前操作文件?就像上传文件一样,通过我的函数传递它,然后从那里输出成为我的“保存”文件?

1 个答案:

答案 0 :(得分:3)

创建自己的过滤器。

http://framework.zend.com/manual/1.12/en/zend.filter.writing_filters.html

class Resize implements Zend_Filter_Interface
{
    public function filter($value)
    {
        // perform some transformation upon $value to arrive on $valueFiltered

        return $valueFiltered;
    }
}

在过滤方法中,您将收到上传文件的路径。在那里调整大小并返回新路径。然后,您可以在上传对象中指定此过滤器:

$upload->addFilter("Resize");

Zend_File_Transfer过滤器的详细说明here