CodeIgniter上传和调整大小问题

4
我花了几天时间根据文档中的示例尝试使其工作,但我似乎还是缺少了一些东西或者我太“蠢”了!
我有一个CMS应用程序,用户上传图像以在非常固定的布局中显示。我们不想限制上传图像的文件大小,而是希望在到达后对其进行“处理”。
图像需要为615px宽,但直接从数码相机上传的某些图像为2500X2000及更大,因此这非常关键。
我从手册中拼凑出代码,并成功将图像上传到CMS应用程序中的文件夹中。然而,图像没有被调整大小。
如果我最终调整大小,我的计划是使用jCrop向用户呈现图像进行裁剪(最终图像必须为615X275,可能需要在调整大小后裁剪高度),然后使用codeigniter将图像FTP到他们网站的amenities文件夹中,使用原始名称。
我将非常感谢在这个问题上的任何帮助!
以下是我的代码:
function do_feature_upload() {
    // 获取图片名称
    $imageName = $this->uri->segment(3);
// 设置上传文件的路径和类型 $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']);
// 在图片大小调整完成后,显示 jCrop 选项
// 将图片上传到最终目标位置 $this->load->view('upload_success', $data); } }

尝试在调整大小函数中删除base_url()。如果我没错的话,路径(source_image和new_image)必须是相对路径而不是绝对路径。 - trix
2个回答

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");
    }
}

所以我应该将这个放在模型中,并像这样调用它:$this->content->resizeImageToDimensions($image_data['full_path'], $newWidth, $newHeight); - jgravois
就像这样,确保图像路径指向正确的位置(可能需要一些调整才能使其正常工作)。并不是非要将其放在模型中,但这是我喜欢组织事物的方式(我通常采用全面的MVC方法……任何与数据/文件存储/操作有关的都放在模型中)。这实际上没有什么理由不能成为自己的库或图像库扩展。无论哪种方式最好都可以。但是最好将其从控制器中剥离,因为在所有情况下您都应该拥有轻量级的控制器。 - treeface

1

几个月前我也遇到了同样的问题,但很遗憾,我没能解决它。我在这里CodeIgniter论坛上发布了同样的问题,但没有人能帮助我。

最终我使用了timthumb脚本,它很好用,但远非理想之选 :(

所以,如果你很着急,我强烈建议使用timthumb。如果你有时间去投资它,祝你好运,并请分享!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接