PHPの開発環境を作るためのリンク集と実施例

controller
function send_name() {
        $form = $this->input->post();
        if (!isset($_FILES['PATH'])) {
            $path = "";
        } else {
            $config = [];
            $config['upload_path'] = FCPATH . 'images/user';
            $config['file_ext_tolower'] = TRUE; //大文字小文字判別
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['encrypt_name'] = TRUE; //乱数名前にする。
            $config['max_size'] = 5000;
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('PATH')) {
                $error = [
                    'error' => $this->upload->display_errors("", ""),
                ];
                $this->json_library->_echo(201, $form, $error, $this->upload->display_errors("", ""));
            } else {
                $data = $this->upload->data();
                $path = "user/" . $data['file_name'];
                if ($data) {

                    $this->Registration_model->orientationFixedImage($data["full_path"]);

                    $config = array(
                        "source_image" => $data["full_path"],
                        "maintain_ratio" => true,
                        "width" => 200,
                        "height" => 200
                    );
                    // イメージライブラリは次のように引数をとります。
                    // 引数はライブラリ読み込み前に定義しておきます。
                    $this->load->library('image_lib');
                    $this->image_lib->initialize($config);
                    if (!$this->image_lib->resize()) {
                        echo $this->image_lib->display_errors();
                    }
                }
            }
        }

/*

db操作は割愛。

*/
    }
model
public function orientationFixedImage($input) {
        $exif = @exif_read_data($input);
        if (empty($exif['Orientation'])) {
            return FALSE;
        }

        $this->load->library('image_lib');

        $config['image_library'] = 'gd2';
        $config['source_image'] = $input;

        $orientations = array();

        switch ($exif['Orientation']) {
            case 1: // no need to perform any changes
                break;

            case 2: // horizontal flip
                $orientations[] = 'hor';
                break;

            case 3: // 180 rotate left
                $orientations[] = '180';
                break;

            case 4: // vertical flip
                $orientations[] = 'ver';
                break;

            case 5: // vertical flip + 90 rotate right
                $orientations[] = 'ver';
                $orientations[] = '270';
                break;

            case 6: // 90 rotate right
                $orientations[] = '270';
                break;

            case 7: // horizontal flip + 90 rotate right
                $orientations[] = 'hor';
                $orientations[] = '270';
                break;

            case 8: // 90 rotate left
                $orientations[] = '90';
                break;

            default:
                break;
        }

        foreach ($orientations as $orientation) {
            $config['rotation_angle'] = $orientation;
            $this->image_lib->initialize($config);

            $this->image_lib->rotate();
        }
         $this->image_lib->clear();
    }

すでに$this->load->library('upload')されてれば$this->image_lib->initialize($config);

[紹介元] PHPタグが付けられた新着投稿 – Qiita PHPの開発環境を作るためのリンク集と実施例

関連記事