CodeIgniter上传和调整大小问题

时间:2010-12-01 16:52:00

标签: codeigniter image-manipulation

我花了几天时间根据文档中的示例尝试完成这项工作,但我遗漏了一些东西,或者我只是愚蠢!

我有一个CMS应用程序,用户上传图像以便以非常固定的布局显示。我们不希望限制上传图像的文件大小,而是在它到达后“处理”它。

图像需要宽615像素,但直接从数码相机上传的部分图像是2500X2000且更大,所以这很重要。

我拼凑了手册中的代码,图片已成功上传到CMS应用中的文件夹。但是,图像未调整大小。

如果我想重新调整尺寸,我的计划是将图像呈现给用户使用jCrop进行裁剪(最终图像为615X275,并且可能必须在调整大小后裁剪高度)然后使用codeigniter使用原始名称将图像FTP到其网站的设施文件夹。

我将非常感谢这件事的任何帮助!

这是我的代码:

function do_feature_upload() {
        $imageName = $this->uri->segment(3);
        //echo $imageName;

        // Where the file is going to be placed
        $config['upload_path'] =  "./uploads/".$_SESSION['dbPropNumber'];
        $config['allowed_types'] = 'jpg|jpeg';
        $config['max_size'] = '0';
        $config['file_name'] = $imageName.'.jpg';
        $config['overwrite'] = 'TRUE';

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

        if ( ! $this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());

            $error['propertyDropdown'] = $_SESSION['propertyDropdown'];
            $error['username'] = $_SESSION['username'];
            $error['dbPropNumber'] = $_SESSION['dbPropNumber'];
            $error['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']);

            $this->load->view('upload_AmenityImage', $error);
        } else {
            $image_data = $this->upload->data();

            $origWidth = $image_data['image_width'];
            $origHeight = $image_data['image_height'];
            $newWidth = 615;
            $newHeight = $newWidth*$origHeight/$origWidth;

            $resize = array(
                'image_library'=>'gd2',
                'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg',
                'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg',
                'create_thumb' => FALSE,
                'maintain_ratio'=>FALSE,
                'width'=>$newWidth,
                'height'=>$newHeight
            );

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

            $data = array('upload_data' => $this->upload->data());
            $data['propertyDropdown'] = $_SESSION['propertyDropdown'];
            $data['username'] = $_SESSION['username'];
            $data['dbPropNumber'] = $_SESSION['dbPropNumber'];
            $data['propertyName'] = $this->content->getPropertyName($_SESSION['dbPropNumber']);

            //Present jCrop option after image is resized

            // FTP to final destination

            $this->load->view('upload_success', $data);
        } // end if
    } // end function

2 个答案:

答案 0 :(得分:7)

我不完全确定您的代码出了什么问题,但这是我编写的模型函数,用于调整图像大小以适应精确的目标高度目标宽度。仔细阅读它,看看你是否无法找到解决方案。

$this->prefix是我使用的类中的属性,因此我不必继续写出文件的目录。它看起来像这样:

$this->prefix = FCPATH.'uploads'.DIRECTORY_SEPARATOR;

图片缩放器

/**
 * Resizes an image to fit exact dimensions
 * 
 * @param string    filename
 * @param int      target_width
 * @param int      target_height
 * 
 * @return array('success' ? null : 'error')
 */
function resizeImageToDimensions($filename, $target_width=700, $target_height=399)
{
    $file_type = $this->getFileType($this->prefix.$filename);

    if (!$file_type || $file_type != 'image')
        return array('success'=>false, 'error'=>"This file doesn't exist or isn't an image");

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

    list($width, $height) = getimagesize($this->prefix.$filename);
    $current_ratio = $width/$height;
    $target_ratio = $target_width/$target_height;
    $config['source_image'] = $this->prefix.$filename;

    if ($current_ratio > $target_ratio)
    {
        //resize first to height, maintain ratio
        $config['height'] = $target_height;
        $config['width'] = $target_height * $current_ratio;
        $this->image_lib->initialize($config);

        if (!$this->image_lib->resize())
            return array('success'=>false, 'error'=>"There was an error while resizing this image");

        //then crop off width
        $config['width'] = $target_width;
        $config['maintain_ratio'] = false;
        $this->image_lib->initialize($config);

        if ($this->image_lib->crop())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while cropping this image");
    }
    else if ($current_ratio < $target_ratio)
    {
        //resize first to width, maintain ratio
        $config['width'] = $target_width;
        $config['height'] = $target_width / $current_ratio;
        $this->image_lib->initialize($config);

        if (!$this->image_lib->resize())
            return array('success'=>false, 'error'=>"There was an error while resizing this image");

        //then crop off height
        $config['height'] = $target_height;
        $config['maintain_ratio'] = false;
        $this->image_lib->initialize($config);

        if ($this->image_lib->crop())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while cropping this image");
    }
    else {
        $config['width'] = $target_width;
        $config['height'] = $target_height;
        $this->image_lib->initialize($config);

        if ($this->image_lib->resize())
            return array('success'=>true);
        else
            return array('success'=>false, 'error'=>"There was an error while resizing this image");
    }
}

答案 1 :(得分:1)

几个月前我遇到了同样的问题,遗憾的是,我无法弄明白。我发布了同样的问题hereCodeIgniter's forums,但没有人可以帮助我。

我最终使用timthumb脚本,这很棒,但远非理想:(

所以,如果你匆忙,我强烈建议你使用timthumb。如果您有时间投资,我希望您最好,请分享!