CodeIgniter上传和调整大小问题

发布于 2024-10-05 14:52:57 字数 2807 浏览 0 评论 0原文

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

我有一个 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

I have spent days trying to make this work based on the examples in the documentation but I am missing something or I am just STUPID!

I have a CMS application where users upload an image for display in a very fixed layout. We do not want to limit the file size of the uploaded image but would rather "process" it after it arrives.

The image needs to be 615px wide but some of the images uploaded directly from digital cameras are 2500X2000 and bigger so this is CRITICAL.

I pieced together the code from the manual and the image is successfully being uploaded to a folder within the CMS app. However, the image is NOT being resized.

If I ever get it to re-size, my plan is to present the image to the user for cropping using jCrop (the final image HAS to be 615X275 and it probably has to be cropped for height after resizing) and then use codeigniter to FTP the image to their site's amenities folder using the original name.

I will appreciate any help in this matter!

Here's my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-10-12 14:52:57

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

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

I'm not entirely sure what's going wrong with your code, but here's a model function I wrote for resizing images to fit an exact target height and target width. Read through it and see if you can't figure out the solution.

$this->prefix is a property in my class that I use so I don't have to keep writing out the directory of the file. It looks like this:

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

Image resizer

/**
 * 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");
    }
}
假扮的天使 2024-10-12 14:52:57

几个月前我遇到了同样的问题,可悲的是,我无法弄清楚。我在此处CodeIgniter 论坛 但没有人可以帮助我。

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

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

I had the same problem a few months back and sadly, I wasn't able to figure it out. I've posted the same question here and in CodeIgniter's forums but no one could help me.

I ended up using timthumb script, which is great, but nowhere near ideal :(

So, if you're in a rush, I'd strongly recommend using timthumb. If you have some time to invest on it, I wish you the best and please, share!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文