【问题标题】:Trying to resize an image twice in a function and it's only resizing it once试图在一个函数中调整图像大小两次,它只调整一次大小
【发布时间】:2011-12-26 17:08:33
【问题描述】:

我正在尝试让用户上传文件,然后我的函数会将照片调整为 800x600 之类的大小,然后将该文件调整为 165x165。使用我的代码,它只会将其大小调整为 165x165,但如果我将该部分注释掉,它会将其大小调整为 800x600。

我该怎么做才能调整两者的大小?

   /**
    * addPhoto - function which shows the add photo page
    */
    function addPhoto($album)
    {   
        $album = rawurldecode($album);

        ini_set('upload_max_filesize', '4M');  
        ini_set('post_max_size', '12M');  
        ini_set('max_input_time', 300);  
        ini_set('max_execution_time', 300); 

        $config['upload_path'] = './assets/images/photoAlbums/'.$album.'/';
        $config['allowed_types'] = 'jpg|jpeg|pjpeg';
        $config['max_size'] = '4096';   // that's 4MBs
        $config['max_width'] = '4000';
        $config['max_height'] = '3000';
        $config['remove_space'] = TRUE;
        $config['overwrite'] = TRUE;

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload('userFile')) { // if there are errors uploading the file...
            $content_data['error'] = array('error' => $this->upload->display_errors());


        } else {    // else there are NO errors, we need to resize it, and ditch that huge ass original


        $image = $this->upload->data(); 
        $uploadedFile = $image['file_name'];

        $this->load->library('image_lib');

        $thumbConfig['image_library'] = 'gd2';
            $thumbConfig['source_image'] = $image['full_path']; 
        $thumbConfig['create_thumb'] = FALSE;
        $thumbConfig['maintain_ratio'] = TRUE;
        $thumbConfig['width'] = 800;
        $thumbConfig['height'] = 600;

        $this->image_lib->initialize($thumbConfig);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $this->image_lib->clear();

        $thumbConfig['image_library'] = 'gd2';
        $thumbConfig['source_image'] = $image['full_path'];
        $thumbConfig['create_thumb'] = TRUE;
        $thumbConfig['maintain_ratio'] = FALSE;
        $thumbConfig['width'] = 165;
        $thumbConfig['height'] = 165;

        $this->image_lib->initialize($thumbConfig);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $content_data['firstName'] = $this->session->userdata('firstName');
        $data['sess'] = $this->session;
        $data['content'] = $this->load->view('member/addPhoto_success', $content_data, true);
        $this->load->view('template/admin', $data);
        }
    }

【问题讨论】:

    标签: php codeigniter image-processing


    【解决方案1】:

    我知道这是一个非常古老的问题,但我遇到了同样的问题。

    $config['create_thumb'] = true
    

    没有创建新文件。

    添加拇指标记配置:

    $config['thumb_marker'] = '_thumb'
    

    工作。

    似乎_thumb 不是thumb_marker 的默认值。

    【讨论】:

      【解决方案2】:

      如果你想创建两个新的调整大小的图像并保持原样,你需要添加

      $config['create_thumb'] = TRUE;
      

      $config['new_image'] = '/path/to/new_image.jpg';
      

      到第一部分。

      根据您的代码,您将使用 800x600 版本覆盖原始文件,然后创建 165x165 缩略图。

      【讨论】:

      • 我阅读了我的代码,因为它会将上传的图片调整为 800x600,然后将该图像作为源并基于 800x600 文件创建一个拇指 165x165,因为我在第二个配置中具有 create_thumb = true。无论哪种情况,new_image 都是票。
      【解决方案3】:

      第二次运行 image_lib 时,您将希望通过指定要创建的新文件来避免覆盖文件: 之后:

      $thumbConfig['height'] = 165;
      

      添加:

      $thumbConfig['new_image'] = $image['file_path'] . "thumb_" . $image['file_name'];
      

      因此,如果您调整大小的 800 x 600 图像位于 /my/upload/path/foo.jpg, 这将在 /my/upload/path/thumb_foo.jpg 处创建一个拇指。

      【讨论】:

      • 在这里聚会真的很晚了,但用户指南说“create_thumb”设置将创建一个缩略图(并保留原件)。在这种情况下,为什么必须指定 new_image? “create_thumb”的存在应该为我们这样做吗?
      【解决方案4】:

      我认为您在第一次调整大小时错过了这一行:

      $thumbConfig['source_image'] = $image['full_path'];
      

      【讨论】:

      • 我的代码中实际上有这行,我一定是在发布这个问题时删除了我的 cmets 时不小心删除了它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多