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

时间:2014-03-30 01:33:16

标签: php codeigniter class gd2

我创建了一个只上传1张图片的图书馆,根据路径,宽度和高度裁剪它。但我无法弄清楚我如何能够裁剪尽可能多的图像并返回它创建的所有文件名的数组。这是我的图书馆,让我知道是否有一个好的方法。感谢

<?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;
        }
    }
}

更新:我明白了,这是课程,以及如何将它用于任何试图做某事的人。这个课程的作用是上传图像,并允许您根据需要创建原始上传的大小。

[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'];
    }
}

用法

$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 个答案:

答案 0 :(得分:1)

将裁剪方法与上传方法分开。

上传后,在上传的文件中单独执行裁剪两次。

删除上传的临时资料。文件。

结束。