如何在PHP中自动调整上传的照片大小?

时间:2013-05-15 11:41:48

标签: php resize kohana

如何自动调整上传到此文件夹的图片大小:'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);

1 个答案:

答案 0 :(得分:1)

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

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

Image::factory($filename)
    ->resize(300, NULL, Image::AUTO)
    ->save($your_save_path);