【问题标题】:Image resize in codeigniter在codeigniter中调整图像大小
【发布时间】:2018-02-14 13:56:13
【问题描述】:

我想使用两个文件输入上传多张图片并调整它们的大小。
1。原始图像
2。 100×100
3。 400×200
一切正常,但图像库无法正确调整大小。它以原始大小上传图像。我检查了以下内容。
1。图像库只加载一次。我在 __construct() 中加载。
2。在初始化之前清除图像库。 $this->image_lib->clear();

function do_upload($name,$shop_code) {


        $files = $_FILES;
        if ($name == 'pre_photo_array') {
            $photo_name = 'pre_photo_array';
        } else {
            $photo_name = 'post_photo_array';
        }
        $cpt = count($_FILES[$name]['name']);
        --$cpt;

        for ($i = 0; $i < $cpt; $i++) {

            $_FILES[$photo_name]['name'] = $files[$photo_name]['name'][$i];
            $_FILES[$photo_name]['type'] = $files[$photo_name]['type'][$i];
            $_FILES[$photo_name]['tmp_name'] = $files[$photo_name]['tmp_name'][$i];
            $_FILES[$photo_name]['error'] = $files[$photo_name]['error'][$i];
            $_FILES[$photo_name]['size'] = $files[$photo_name]['size'][$i];


            if ($photo_name == 'pre_photo_array') {
                $config['upload_path'] = './assets/images/shop_images/pre_pic/';
            } else {
                $config['upload_path'] = './assets/images/shop_images/post_pic/';
            }

            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['overwrite'] = FALSE;
            $config['file_name'] = $shop_code.'_'.time();


            $this->upload->initialize($config);
            if (!$this->upload->do_upload($photo_name)) {

                $error = array('error' => $this->upload->display_errors());

                $file_path[] = ' ';
            } else {

                $upload_data = $this->upload->data();

                if ($name == 'pre_photo_array') {
                    $imgdata=exif_read_data('assets/images/shop_images/pre_pic/'.$upload_data['file_name'], 'IFD0');
                    $source_image_path = getimagesize('assets/images/shop_images/pre_pic/'.$upload_data['file_name']);
                    $source_image = 'assets/images/shop_images/pre_pic/'.$upload_data['file_name'];
                    $Pre_Thumbnail_new_image = 'assets/images/shop_images/pre_pic/Thumbnail/'.$upload_data['file_name'];
                    $Pre_ppt_new_image = 'assets/images/shop_images/pre_pic/PPT/'.$upload_data['file_name'];
                } else {
                    $imgdata=exif_read_data('assets/images/shop_images/post_pic/'.$upload_data['file_name'], 'IFD0');
                    $source_image_path = getimagesize('assets/images/shop_images/post_pic/'.$upload_data['file_name']);
                    $source_image = 'assets/images/shop_images/post_pic/'.$upload_data['file_name'];
                    $Post_Thumbnail_new_image = 'assets/images/shop_images/post_pic/Thumbnail/'.$upload_data['file_name'];                    
                    $Post_ppt_new_image = 'assets/images/shop_images/post_pic/PPT/'.$upload_data['file_name'];
                }


                 $rotation_angle = 0;

                switch(isset($imgdata['Orientation'])) {
                    case 3:
                        $rotation_angle='180';
                        break;
                    case 6:
                        $rotation_angle='90';
                        break;
                    case 8:
                        $rotation_angle='90';
                        break;
                }

                if($name == 'pre_photo_array') {
                    $Thumbnail_new_image = $Pre_Thumbnail_new_image;
                    $ppt_new_image = $Pre_ppt_new_image;
                } else {
                    $Thumbnail_new_image = $Post_Thumbnail_new_image;
                    $ppt_new_image = $Post_ppt_new_image;
                }

                // Create Thumbnail
                $Thumbnail['image_library'] = 'gd2';
                $Thumbnail['source_image'] = $source_image;
                $Thumbnail['new_image'] = $Thumbnail_new_image;
                $Thumbnail['create_thumb'] = TRUE;
                $Thumbnail['thumb_marker'] = FALSE;
                $Thumbnail['maintain_ratio'] = TRUE;
                $Thumbnail['width'] = 100;
                $Thumbnail['height'] = 100;
                $Thumbnail['rotation_angle'] = $rotation_angle;

                $this->image_lib->clear();
                $this->image_lib->initialize($Thumbnail);
                // resize image
                $this->image_lib->resize();
                $this->image_lib->rotate();
                // handle if there is any problem
                if(!$this->image_lib->resize()) {
                    echo "if";
                    echo $this->image_lib->display_errors();exit;
                }


                if($source_image_path[0] > $source_image_path[1]){
                    $width = 400;
                    $height = 200;
                }else{
                    $width = 200;
                    $height = 400;
                }

                // Create PPt image
                $ppt['image_library'] = 'gd2';
                $ppt['source_image'] = $source_image;
                $ppt['new_image'] = $ppt_new_image;
                $ppt['create_thumb'] = FALSE;
                $ppt['thumb_marker'] = FALSE;
                $ppt['maintain_ratio'] = TRUE;
                $ppt['width'] = 400;
                $ppt['height'] = 200;
                $ppt['rotation_angle']=$rotation_angle;


                $this->image_lib->clear();
                $this->image_lib->initialize($ppt);
                // resize image
                $this->image_lib->resize();
                $this->image_lib->rotate();
                // handle if there is any problem
                if(!$this->image_lib->resize()) {
                    echo $this->image_lib->display_errors();exit;
                }


                if($photo_name == 'pre_photo_array') {
                    $path = 'assets/images/shop_images/pre_pic/';
                } else {
                    $path = 'assets/images/shop_images/post_pic/';
                }
                $file_path[] = $path.$upload_data['file_name'];

            }
        }

        return $file_path;
    }

}

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    我在 CodeIgniter 示例中添加我的上传图像并创建多个缩略图大小。希望它可以帮助你。

    //initialize the path where you want to save your images
    function __construct(){
    parent::__construct();
    //return the full path of the directory
    //make sure these directories have read and write permessions
        $this->original_path = realpath(APPPATH.'../uploads/original');
        $this->resized_path = realpath(APPPATH.'../uploads/resized');
        $this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
    }
    
    function do_upload(){
        $this->load->library('image_lib');
        $config = array(
            'allowed_types'     => 'jpg|jpeg|gif|png', //only accept these file types
            'max_size'          => 2048, //2MB max
            'upload_path'       => $this->original_path //upload directory
    );
    
    $this->load->library('upload', $config);
    $image_data = $this->upload->data(); //upload the image
    
    //your desired config for the resize() function
    $config = array(
        'source_image'      => $image_data['full_path'], //path to the uploaded image
        'new_image'         => $this->resized_path, //path to
        'maintain_ratio'    => true,
        'width'             => 128,
        'height'            => 128
    );
    
    //this is the magic line that enables you generate multiple thumbnails
    //you have to call the initialize() function each time you call the resize()
    //otherwise it will not work and only generate one thumbnail
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear();
    
    $config = array(
        'source_image'      => $image_data['full_path'],
        'new_image'         => $this->thumbs_path,
        'maintain_ratio'    => true,
        'width'             => 36,
        'height'            => 36
    );
    
    //here is the second thumbnail, notice the call for the initialize() function again
    $this->image_lib->initialize($config);
    $this->image_lib->resize();
    $this->image_lib->clear();
    }
    

    【讨论】:

    • 我想我也做过同样的事情。如果我的代码有任何错误,请告诉我。
    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2018-06-17
    • 2011-06-28
    • 2019-10-31
    • 2011-04-26
    相关资源
    最近更新 更多