EXIF ориентация CodeIgniter - PullRequest
0 голосов
/ 14 мая 2018

Я пытаюсь решить проблему ориентации при загрузке изображений с мобильных устройств. Фотографии загружаются, но коррекция поворота не применяется. Использование класса манипулирования изображениями из Codeigniter. Заранее благодарю за ваши предложения.

Вот мой код:

public function changeimage() {
        $this->load->library('upload');
        $sid = $this->session->userdata['user']['id'];
        $files = $_FILES;
        if (!empty($_FILES['image']['name'])) {
            $format = explode('.', $files['image']['name']);
            $format = end($format);
            $cimagename = md5(uniqid(mt_rand())) . ".$format";
            $_FILES['image']['name'] = $cimagename;
            $_FILES['image']['type'] = $files['image']['type'];
            $_FILES['image']['tmp_name'] = $files['image']['tmp_name'];
            $_FILES['image']['error'] = $files['image']['error'];
            $_FILES['image']['size'] = $files['image']['size'];
            $date = date('Y/m/d H:i:s', time());
            $date1 = date('Y/m/d ', strtotime($this->input->post('createdon')));
            $this->upload->initialize($this->set_upload_options($sid, $date1));
            if (!$this->upload->do_upload("image")) {
                $data['error'] = true;
                $data['errormsg'] = "Something went wrong";
                echo $data['errormsg'];
            } else {
                $data = array("upload_data" => $this->upload->data());
                $image_info = $this->upload->data();
                $this->load->library('image_lib');
                $config['image_library'] = 'gd2';
                $config['source_image'] = $files;
                $config['new_image'] = $files;
                $exif = exif_read_data($config['source_image']);
                if ($exif && isset($exif['Orientation'])) {
                    $ort = $exif['Orientation'];
                    if ($ort == 6 || $ort == 5)
                        $config['rotation_angle'] = '270';
                    if ($ort == 3 || $ort == 4)
                        $config['rotation_angle'] = '180';
                    if ($ort == 8 || $ort == 7)
                        $config['rotation_angle'] = '90';
                }
                $this->image_lib->initialize($config);
                if (!$this->image_lib->rotate()) {
                    echo $this->image_lib->display_errors();
                }
                $this->image_lib->clear();
                $this->load->library("image_lib");
                $config['image_library'] = "gd2";
                $config['library_path'] = '/login/application/libraries/';
                $config['source_image'] = "assets/userimages/" . $image_info['file_name'];
                $config['maintain_ratio'] = FALSE;
                $config['rotation_angle'] = '';
                $sizes = array(array("name" => "thumb", "width" => "200", "height" => "227"));
                foreach ($sizes as $size) {
                    $config['new_image'] = "assets/userimages/" . $size['name'] . "_" . $image_info['file_name'];
                    $config['width'] = $size['width'];
                    $config['height'] = $size['height'];
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                }
            }
        }
    }

1 Ответ

0 голосов
/ 20 сентября 2018

У меня эта библиотека работала:

class Rotateimage {

    /*
     * @param string $setFilename - Set the original image filename
     * @param array $exifData - Set the original image filename
     * @param string $savedFilename - Set the rotated image filename
     */

    private $setFilename    = "";
    private $exifData       = "";
    private $degrees        = "";

    public function __construct(){

    }

    public function setfile($setFilename){
        try{
            if(!file_exists($setFilename)){
                throw new Exception('File not found.');
            } 
            $this->setFilename = $setFilename;
        } catch (Exception $e ) {
            die($e->getMessage());
        } 
    }

    /*
     * EXTRACTS EXIF DATA FROM THE JPEG IMAGE
     */
    public function processExifData(){
        $orientation = 0;
        $this->exifData = exif_read_data($this->setFilename);
        foreach($this->exifData as $key => $val){
            if(strtolower($key) == "orientation" ){
                $orientation = $val;
                break;
            }
        }
        if( $orientation == 0 ){
            $this->_setOrientationDegree(1);
        }
        $this->_setOrientationDegree($orientation); 
    } 

    /*
     * DETECTS AND SETS THE IMAGE ORIENTATION
     * Orientation flag info  http://www.impulseadventure.com/photo/exif-orientation.html
     */
    private function _setOrientationDegree($orientation){
       switch($orientation):
           case 1: 
               $this->degrees = 0;
               break;
           case 8:
               $this->degrees = 90;
               break;
           case 3:
               $this->degrees = 180;
               break;
           case 6:
               $this->degrees = 270;
               break;
       endswitch;
       $this->_rotateImage();
    }  


    /*
     * ROTATE THE IMAGE BASED ON THE IMAGE ORIENTATION
     */
    private function _rotateImage(){
        if($this->degrees < 1 ){
            return FALSE;
        }
        $image_data = imagecreatefromjpeg($this->setFilename);
        return imagerotate($image_data, $this->degrees, 0);  
    } 


    /*
     * SAVE THE IMAGE WITH THE NEW FILENAME
     */
    public function savedFileName($savedFilename){
        if($this->degrees < 1 ){
            return false;   
        }
        $imageResource = $this->_rotateImage();
        if($imageResource == FALSE){
            return false;   
        }
        imagejpeg($imageResource, $savedFilename);  
    } 

}
...