doutan3192 2014-03-30 01:33
浏览 71
已采纳

如何使用codeigniter上传原始图像后裁剪和接收两个图像

I created a library that just uploads 1 image, crops it given the path, width, and height. But i cant figure out how I can have it crop as many images as i want and return an array of all the filenames it created. Here is my library, let me know if there is a good approach. thanks

<?php
/*
    Usage:
    -----------------------------------------------------------------------
    $this->load->library('up_image');
    $this->logo = $this->up_image->upload_image(upload_path, width, height);
*/
class up_image
{
    /**
    *   Upload Image Method
    *   ----------------------
    *   @Param: Upload Path
    *   @Param: Width: 
    *   @Param: Height: 
    **/
    public function upload_image($path, $width, $height)
    {
        //load codeigniter instant to load stuff
        $CI =& get_instance();
        $config1 = array(
            'allowed_types' => 'jpg|jpeg|png',
            'upload_path' => $path,
            'max_size' => 4000,
            'remove_spaces' => TRUE,
            'encrypt_name' => TRUE
        );


        $CI->load->library('upload', $config1);
        $CI->upload->display_errors('<p class="errors">', '</p>');

        //if upload do something
        if($CI->upload->do_upload())
        {
            $image_data = $CI->upload->data();

            $config2 = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'],
                'new_image' => $path . '/' . $image_data['file_name'],
                'maintain_ratio' => TRUE,
                'width' => $width,
                'height' => $height
            );


            $CI->load->library('image_lib', $config2);
            $CI->image_lib->resize();
            return $image_data['file_name'];
        }
        else
        {
            return FALSE;
        }
    }
}

Update: I figured it out, here is the class and how to use it for anyone who is trying to do the something. What this class does is uploads the image and lets you create as many sizes of the original upload as you want.

[CLASS]

<?php
/*
Author: Sarmen B
URL: sarmenb.com

*/
class up_image
{
    var $path = '';

    /**
    *   Upload Image
    *   ----------------------
    *   @Param: Upload Path
    *   @Param: Width
    *   @Param: Height
    **/
    public function upload_image()
    {
        //load codeigniter instant to load stuff
        $CI =& get_instance();
        $config = array(
            'allowed_types' => 'jpg|jpeg|png',
            'upload_path' => $this->path,
            'max_size' => 4000,
            'remove_spaces' => TRUE,
            'encrypt_name' => TRUE
        );


        $CI->load->library('upload', $config);
        $CI->upload->display_errors('<p class="errors">', '</p>');

        //if upload do something
        if($CI->upload->do_upload())
        {
            //returns the images data as an array
            return $CI->upload->data();
        }
        else
        {
            return FALSE;
        }
    }


    /**
    *   Resize the image
    *   ----------------------
    *   @Param: $data data returned from upload_image()
    *   @Param: Width
    *   @Param: Height
    **/
    public function resize($data, $width, $height)
    {
        $CI =& get_instance();

        $config = array(
            'image_library' => 'gd2',
            'source_image' => $data['full_path'],
            'new_image' => $this->path . '/' . $data['file_name'],
            'maintain_ratio' => TRUE,
            'width' => $width,
            'height' => $height,
            'create_thumb' => FALSE
        );

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

        $CI->image_lib->clear();
        unset($config);
        return $data['file_name'];
    }
}

Usage

$img1 = $this->up_image->upload_image();
$thumb = $this->up_image->resize($img1, 140, 120);

$img2 = $this->up_image->upload_image();
$large = $this->up_image->resize($img2, 420, 360);
  • 写回答

1条回答 默认 最新

  • dongtuo4723 2014-03-30 03:15
    关注

    Separate the crop method from the upload method.

    After upload, perform crop twice, separately on the uploaded file.

    Delete uploaded temp. file.

    Finish up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!