【问题标题】:CodeIgniter Upload and Resize ProblemCodeIgniter 上传和调整大小问题
【发布时间】:2011-05-18 15:03:47
【问题描述】:

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

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

图像需要 615 像素宽,但直接从数码相机上传的一些图像是 2500X2000 或更大,因此这很关键。

我将手册中的代码拼凑在一起,图像已成功上传到 CMS 应用程序中的文件夹。但是,图像没有被调整大小。

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

我将不胜感激在这件事上的任何帮助!

这是我的代码:

功能 do_feature_upload() { $imageName = $this->uri->segment(3); //回显 $imageName; // 文件要放在哪里 $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); } 别的 { $image_data = $this->上传->数据(); $origWidth = $image_data['image_width']; $origHeight = $image_data['image_height']; $新宽度 = 615; $newHeight = $newWidth*$origHeight/$origWidth; $resize = 数组( 'image_library'=>'gd2', 'source_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'.jpg', 'new_image'=>base_url().'uploads/'.$_SESSION['dbPropNumber'].'/'.$imageName.'1.jpg', 'create_thumb' => 假, 'maintain_ratio'=>假, '宽度'=>$newWidth, '高度'=>$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选项 // FTP到最终目的地 $this->load->view('upload_success', $data); } // 万一 } // 结束函数

【问题讨论】:

  • 尝试移除 resize 函数中的 base_url()。如果我是正确的路径(source_image 和 new_image)必须是相对的而不是绝对的。
  • 工作就像一个魅力!!!!谢谢!!!

标签: codeigniter image-manipulation


【解决方案1】:

我不完全确定您的代码出了什么问题,但这是我编写的一个模型函数,用于调整图像大小以适应精确的目标高度目标宽度。通读一遍,看看你是否想不出解决办法。

$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['$image_data['full_path'],'],$newWidth, $新高度);
  • 类似的东西,除了确保图像路径指向正确的位置(可能需要一些修补才能使其正常工作)。将其保存在模型中本身并不是真正必要的,但这是我喜欢组织事物的方式(我通常对 MVC 采取整体方法......任何处理数据/文件存储/操作的东西都在模型中)。没有真正的理由为什么它不能只是它自己的库或 image_lib 上的扩展。什么最适合你。但是,是的,最好不要将其置于控制器之外,因为在所有情况下,您都应该使用瘦控制器。
【解决方案2】:

几个月前我遇到了同样的问题,可悲的是,我无法弄清楚。我在hereCodeIgniter's forums 中发布了同样的问题,但没有人可以帮助我。

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

所以,如果您赶时间,我强烈建议您使用 timthumb。如果您有时间投资,我祝您一切顺利,请分享!

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2013-09-12
    • 2015-07-30
    • 2014-01-09
    • 2015-06-19
    • 2020-04-12
    相关资源
    最近更新 更多