-2

如何自动调整上传到此 foder 的图像大小:'assets/media/':

<?php defined('SYSPATH') OR die('No direct access allowed.');

class Uploader_Controller extends Controller_Core {

public function bulkUpload() {
    Kohana::log('debug', 'Start to upload');
    $files = Validation::factory($_FILES)
                    ->add_rules('picture', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png,jpeg]', 'upload::size[10M]');
    Kohana::log('debug', 'Start to validate');
    if ($files->validate()) {
        Kohana::log('debug', 'validate passed');

        $filename = upload::save('picture');
        $thumbSize = Kohana::config('upload.thumb_size');

        Image::factory($filename)
                ->resize($thumbSize[0], $thumbSize[1], Image::WIDTH)
                ->save(DOCROOT . 'assets/media/thumbs/' . basename($filename));

        $partName = explode('/', $filename);
        $picture = $partName[count($partName) - 1];
        $data['name'] = '';
        $data['picture'] = $picture;
        $data['category_id'] = $this->input->post('category_id', 0);
        $data['description'] = '';
        ;
        $data['user_id'] = $this->input->post('user_id', 0);
        $pictureModel = new Picture_Model();
        try {
            $photo = $pictureModel->savePicture($data);
            echo url::site('assets/media/' . $picture);
        } catch (Exception $e) {

        }
    }
}
}

我已经添加了这一行但仍然无法正常工作:

$filename->resizeToWidth(300);

4

1 回答 1

1

您没有正确使用 Kohana 图像和上传库。文档中有一些关于如何使用 Kohana 图像上传和调整大小库的示例:

您可以使用以下代码调整图像大小并保存:

Image::factory($filename)
    ->resize(300, NULL, Image::AUTO)
    ->save($your_save_path);
于 2013-05-16T12:29:17.860 回答