Качване на снимка с мобилен телефон (iOS, Android) ориентаци

Ticketa

Registered
Привет, работя под framework YII с компонент Image handler и когато качвам , чрез телефон снимка в платформата ми тя се завърта наобратно.

Към момента проблема го решавам по-следния начин:

PHP:
                            //Read the JPEG image Exif data to get the Orientation value
                            if (function_exists('exif_read_data')) {
                                $exif = exif_read_data($file);
                                $orientation = @$exif['IFD0']['Orientation'];
                                if (!$orientation) {
                                    $orientation = @$exif['Orientation'];
                                }
                                if(!$orientation) return;
                                $source = @imagecreatefromjpeg($file);
                                if(!$source) return;
                                switch ($orientation) {
                                    case 1: // nothing
                                        break;
                                    case 2: // horizontal flip
                                        imageflip($source, IMG_FLIP_HORIZONTAL);
                                        break;
                                    case 3: // 180 rotate left
                                        $modifiedImage = imagerotate($source, 180, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 4: // vertical flip
                                        imageflip($source, IMG_FLIP_VERTICAL);
                                        break;
                                    case 5: // vertical flip + 90 rotate right
                                        imageflip($source, IMG_FLIP_VERTICAL);
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 6: // 90 rotate right
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 7: // horizontal flip + 90 rotate right
                                        imageflip($source, IMG_FLIP_HORIZONTAL);
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 8:    // 90 rotate left
                                        $modifiedImage = imagerotate($source, 90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality    
                                        break;
                                }
                            }
                            //Read the JPEG image Exif data to get the Orientation value


Но това ми създава проблем. Снимката се прикачва правилно , НО в дясно остава ЧЕРНА ивица/линия/бекграунд.

Което разваля снимката.



Прилагам и цялата ми функция за прикачване на снимката:

PHP:
    private function loadImage($file)
    {
        $result = [];


        if ($imageInfo = @getimagesize($file)) {
            $result['width'] = $imageInfo[0];
            $result['height'] = $imageInfo[1];

            $result['mimeType'] = $imageInfo['mime'];

            switch ($result['format'] = $imageInfo[2]) {
                case self::IMG_GIF:
                    if(file_exists($file)) {
                        $gd = @imagecreatefromstring(file_get_contents($file));    
                        if ($gd === false) {
                            throw new Exception ('Invalid image gif format');
                        } else {
                            if ($result['image'] = imagecreatefromgif($file)) {
                                return $result;
                            } else {
                                throw new Exception('Invalid image gif format');
                            }
                        }
                    } else {
                        throw new Exception('Invalid image gif format');
                    }
                    break;
                case self::IMG_JPEG:
                    if(file_exists($file)) {
                        $gd = @imagecreatefromstring(file_get_contents($file));    
                        if ($gd === false) {
                            throw new Exception ('Invalid image jpeg format');
                        } else {
                            
                            
                            //Read the JPEG image Exif data to get the Orientation value
                            if (function_exists('exif_read_data')) {
                                $exif = exif_read_data($file);
                                $orientation = @$exif['IFD0']['Orientation'];
                                if (!$orientation) {
                                    $orientation = @$exif['Orientation'];
                                }
                                if(!$orientation) return;
                                $source = @imagecreatefromjpeg($file);
                                if(!$source) return;
                                switch ($orientation) {
                                    case 1: // nothing
                                        break;
                                    case 2: // horizontal flip
                                        imageflip($source, IMG_FLIP_HORIZONTAL);
                                        break;
                                    case 3: // 180 rotate left
                                        $modifiedImage = imagerotate($source, 180, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 4: // vertical flip
                                        imageflip($source, IMG_FLIP_VERTICAL);
                                        break;
                                    case 5: // vertical flip + 90 rotate right
                                        imageflip($source, IMG_FLIP_VERTICAL);
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 6: // 90 rotate right
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 7: // horizontal flip + 90 rotate right
                                        imageflip($source, IMG_FLIP_HORIZONTAL);
                                        $modifiedImage = imagerotate($source, -90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality
                                        break;
                                    case 8:    // 90 rotate left
                                        $modifiedImage = imagerotate($source, 90, 0);
                                        imagejpeg($modifiedImage, $file, 90);  //save output to file system at full quality    
                                        break;
                                }
                            }
                            //Read the JPEG image Exif data to get the Orientation value
                            
                            if ($result['image'] = @imagecreatefromjpeg($file)) {
                                return $result;
                            } else {
                                throw new Exception('Invalid image jpeg format');
                            }
                        }
                    } else {
                        throw new Exception('Invalid image jpeg format');
                    }
                    break;
                case self::IMG_PNG:
                    if(file_exists($file)) {
                        $gd = @imagecreatefromstring(file_get_contents($file));    
                        if ($gd === false) {
                            throw new Exception ('Invalid image png format');
                        } else {
                            if ($result['image'] = imagecreatefrompng($file)) {
                                return $result;
                            } else {
                                throw new Exception('Invalid image png format');
                            }
                        }
                    } else {
                        throw new Exception('Invalid image png format');
                    }
                    break;
                default:
                    throw new Exception('Not supported image format');
            }
        } else {
            throw new Exception('Invalid image file');
        }


    }


Проблема ми е сходен с на този потребител: https://stackoverflow.com/questions/32135931/image-is-rotated-correctly-on-mobile-device-not-on-desktop
http://i.imgur.com/ARwSUuV.png
http://i.imgur.com/00yj7OJ.png
 
Функцията imagerotate() има някакви проблеми с прозрачността, също и с минусовите градуси и т.н. Ако трябва да гадая бих почнал от там.

Test script:
---------------
This bug happens with the following script :

$resource = imagecreatefrompng('google.png');
var_dump(imagesx($resource));
$resource = imagerotate($resource, -90, 0);
var_dump(imagesy($resource));

Expected result:
----------------
int(364)
int(364)

Actual result:
--------------
int(364)
int(363)
 
@raiden, няма warning.

@teroristd, да. Има проблем с прозрачността. Но не е ли редно снимката, след като се завърти, да си е нормална? Защо остава празно място?
 
Виж дали има възможност да конфигурираш компонента да работи с ImageMagick вместо с GD библиотеката
Предполагам е това? https://www.yiiframework.com/extension/image
 

Горе