superwf 发表于 2009-8-28 13:49:44

图象的resize问题

本帖最后由 superwf 于 2009-8-31 10:40 编辑

上传一个图片时,需要将大图resize两次或多次,比如上传一个大图,生成一个中图与一个小图。只有第一次resize时能成功,当多次resize时,重新加载$this->load->library('image_lib', $config); 这个$config里的宽高参数,就是不灵。后来查到有个$this->image_lib->clear(),于是在每resize一次只有运行一下再重新加载resize的$config,还是不灵。
而且一次上传多个图片的时候,总是第一个图的resize可以成功,然后其他图就resize不了了。

Hex 发表于 2009-8-28 14:07:52

默认 resize 是修改原图,你需要 copy 出多份再 resize。
仔细看一下手册。

superwf 发表于 2009-8-29 00:06:21

本帖最后由 superwf 于 2009-8-29 00:07 编辑

copy文件之后再resize我之前就试过了,以下是测试用的controller

<?php
class Test extends Controller {
      public function index()
      {
                $this->load->view('test');
      }

      public function upload()
      {
                $path = './upload/';
                $new_path = './upload/thumb';
                $config = array(
                        'allowed_types'=> 'gif|jpg|png|jpeg',
                        'upload_path'=> $path,
                        'overwrite'=> false,
                        'encrypt_name'=> true
                );
                #echo realpath($path);
                #print_r($_FILES);
                $this->load->library('upload', $config);
                if( $this->upload->do_upload('image') ) {
                        $file = $this->upload->data();
                        $temp_file = './upload/temp_'.$file['file_name'];
                        //生成temp文件
                        copy($file['full_path'], $temp_file);

                        //resize原文件
                        $file_thumb_1 = './upload/thumb_1_'.$file['file_name'];
                        $config = array(
                              'image_library' => 'gd2',
                              'source_image' => $file['full_path'],
                              'maintain_ratio' => false,
                              'width' => 150,
                              'height'=> 150,
                              'new_image'=> $file_thumb_1
                        );
                        $this->load->library('image_lib', $config);
                        $this->image_lib->resize();

                        //$this->image_lib->clear();
                        //resize temp文件
                        $file_thumb_2 = './upload/thumb_2_'.$file['file_name'];
                        $config = array(
                              'image_library' => 'gd2',
                              'source_image' => $file_thumb_1,
                              'maintain_ratio' => false,
                              'width' => 50,
                              'height'=> 50,
                              'new_image'=> $file_thumb_2
                        );
                        $this->load->library('image_lib', $config);
                        var_dump($this->image_lib->resize());
                        #echo $this->image_lib->display_errors('<p>', '</p>');

                }else {
                        echo $this->upload->display_errors('<p>', '</p>');
                }
      }
}

index用的视图
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title> new document </title>
</head>
<body>
<?php echo form_open_multipart('test/upload');?>
<input name="image" type="file" /><br />
<input type="submit" />
</form>
</body>
</html>

第二次resize结果是true,但是没有生成第二个缩略图。不知道还有什么错误,无奈了...

kazaff 发表于 2009-8-29 08:39:16

关注~{:3_69:}

Hex 发表于 2009-8-29 10:02:29

你这个代码看起来没问题呀,我在 iqwer.com 上基本上就是这么写的。
没有报错信息吗?

superwf 发表于 2009-8-29 15:49:14

本帖最后由 superwf 于 2009-8-29 15:54 编辑

没有报错信息,而且resize第二次时,var_dump($this->image_lib->resize());结果是bool 的true
在第一次和第二次resize之间执行一次$this->image_lib->clear();的话,第二次的就报错了,说没有加载图象的库。看手册上说这个clear的作用就是resize多图时在中间用的,那段话是英文的,我的理解是否有误?
我用的版本就是从网站下的CI的最新版1.71

Hex 发表于 2009-8-29 17:13:38

呵呵,刚才没注意,你不能多次调用 $this->load->library('image_lib', $config);
第一次调用可以,第二次就要用 $this->image_lib->initialize($config);
CI 的类库装载都有规避机制,多次装载无效。

kazaff 发表于 2009-8-29 18:56:54

恩,$this->image_lib->initialize($config);很重要啊

superwf 发表于 2009-8-30 15:09:33

学到啦,又看了一下手册,initialize方法没有写在手册的图象处理类的五个主要的处理函数里,而是夹在一段英文里的,怪不得没看着大意了。
十分感谢!
页: [1]
查看完整版本: 图象的resize问题