Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 图像在移动设备上正确旋转,而不是在桌面上_Php_Iphone_Image_Image Processing - Fatal编程技术网

Php 图像在移动设备上正确旋转,而不是在桌面上

Php 图像在移动设备上正确旋转,而不是在桌面上,php,iphone,image,image-processing,Php,Iphone,Image,Image Processing,我上传的图片有一个奇怪的问题。当我在iPhone和iPad上查看它们时,它们的旋转方向是正确的,但每当我试图在桌面上查看它们时,它们的显示方向是错误的。 我找不到错误,在花了几个小时处理EXIF数据之后,我几乎要放弃了。 在确定了方向之后,我还调整了图像的大小,但这不会干扰其他代码。如果有,我会把它包括在内 我没有足够的声誉上传图片,但这里有一个链接: 以下是我用来上传的代码: $path_parts = pathinfo($_FILES["file"]["name"]); $filepat

我上传的图片有一个奇怪的问题。当我在iPhone和iPad上查看它们时,它们的旋转方向是正确的,但每当我试图在桌面上查看它们时,它们的显示方向是错误的。 我找不到错误,在花了几个小时处理EXIF数据之后,我几乎要放弃了。 在确定了方向之后,我还调整了图像的大小,但这不会干扰其他代码。如果有,我会把它包括在内

我没有足够的声誉上传图片,但这里有一个链接:

以下是我用来上传的代码:

$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));

// Rotate image correctly!
$exif = exif_read_data($image);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']){
        case 1: // nothing
        break;
        case 2: // horizontal flip
        $image = imageflip($image, IMG_FLIP_HORIZONTAL);
        break;
        case 3: // 180 rotate left
        $image = imagerotate($image,180,0);
        break;
        case 4: // vertical flip
        $image = imageflip($image, IMG_FLIP_VERTICAL);
        break;
        case 5: // vertical flip + 90 rotate right
        $image = imageflip($image, IMG_FLIP_VERTICAL);
        $image = imagerotate($image,-90,0);
        break;
        case 6: // 90 rotate right
        $image = imagerotate($image,-90,0);
        break;
        case 7: // horizontal flip + 90 rotate right
        $image = imageflip($image, IMG_FLIP_HORIZONTAL);
        $image = imagerotate($image,-90,0);
        break;
        case 8:    // 90 rotate left
        $image = imagerotate($image,90,0);
        break;
    }
}

switch ($path_parts['extension']) {
    case 'gif' :
    $im = imagecreatefromgif($image);
    break;
    case 'jpg' :
    $im = imagecreatefromjpeg($image);
    break;
    case 'png' :
    $im = imagecreatefrompng($image);
    break;
    case 'bmp' :
    $im = imagecreatefrombmp($image);
    break;
}
if($im){
    imagejpeg($im, $_FILES['file']['tmp_name'], 40);    
}
$image_path = 'd_'.time() . "." . $path_parts['extension']; 
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);
如果你知道为什么它只能在某些平台上正确旋转,我将非常感激


编辑:可能需要澄清的是,图像通常是从智能手机或平板电脑上传的。

有一些错误导致代码无法正常工作。尝试打开以帮助您调试此类问题

  • exif\u read\u data()
    作用于文件,而不是GD资源,因此传递
    $filepath
    而不是
    $image
  • imageflip()
    直接操作资源并返回
    bool
    ,因此将返回值赋给
    $image
    会破坏资源
  • 第二个
    switch()
    语句根本不需要。
    imagecreatefrom_u_;()
否则,方位校正看起来很准确,应该对你有用(我用手机拍摄的各种测试照片都是如此)

以下是更正后的代码:

$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));

// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
    switch ($exif['Orientation']) {
        case 1: // nothing
            break;
        case 2: // horizontal flip
            imageflip($image, IMG_FLIP_HORIZONTAL);
            break;
        case 3: // 180 rotate left
            $image = imagerotate($image, 180, 0);
            break;
        case 4: // vertical flip
            imageflip($image, IMG_FLIP_VERTICAL);
            break;
        case 5: // vertical flip + 90 rotate right
            imageflip($image, IMG_FLIP_VERTICAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 6: // 90 rotate right
            $image = imagerotate($image, -90, 0);
            break;
        case 7: // horizontal flip + 90 rotate right
            imageflip($image, IMG_FLIP_HORIZONTAL);
            $image = imagerotate($image, -90, 0);
            break;
        case 8:    // 90 rotate left
            $image = imagerotate($image, 90, 0);
            break;
    }
}

imagejpeg($image, $_FILES['file']['tmp_name'], 40);

$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);

可能与您所述的问题没有直接关系,但
imageflip()
返回
bool
,而不是资源,因此您不应该将其分配给
$image
,然后尝试
imagerotate()
。哦。正确的。谢谢你,蒂姆!我应该使用哪种方法翻转图像?只需使用
imageflip()
,而不指定返回值。它直接操纵资源。