PHP旋转图像为空白

时间:2016-09-16 22:13:53

标签: php ios image rotation

我正在使用以下php代码来保存来自各种设备的图像。一切正常,除了iPhone图像出现在侧面。我已经找到了一种方法来解决这个问题,方法是在保存之前旋转图像。但是,当我上传图片时,它不会显示在我的网页上,而在我的文件管理器中,它仍会显示在旁边。我是否将错误的文件定位为旋转?或者我使用其他错误的东西? 这是我的代码:

    $file = $_FILES["newsnap"];
    $id = $_SESSION['id'];
    $username = $_SESSION['username'];
    $aboutitems = nl2br(mysqli_real_escape_string($database, $_POST['about-snap']));
    $uploadloc = mkdir("../$username/");
    $image_temp = $_FILES["newsnap"]['tmp_name'];//Temporary location
    $filename = mysqli_real_escape_string($database, htmlentities($file["name"]));


            $sourcePath = $image_temp; //  source path of the file


            $exif = exif_read_data($sourcePath);
            $orientation = $exif['Orientation'];

            switch($orientation) 
                {
                    case 3:
                        $sourcePath = imagerotate($sourcePath, 180, 0);
                        break;
                    case 6:
                        $sourcePath = imagerotate($sourcePath, -90, 0);
                        break;
                    case 8:
                        $sourcePath = imagerotate($sourcePath, 90, 0);
                        break;
                }

            $targetPath = "../$username/$filename"; // Target path where file is to be stored

            move_uploaded_file($sourcePath, $targetPath) ; // Moving Uploaded file

            $added = date("y.m.d");

            mysqli_query($database, "INSERT INTO piqs(userid, chicpiq, aboutpic, added) VALUES('$id', '$targetPath', '$aboutitems', '$added')");

如果没有以下代码,代码可以正常工作。我在下面添加了代码,只是为了旋转横向图像:

            $exif = exif_read_data($sourcePath);
            $orientation = $exif['Orientation'];

            switch($orientation) 
                {
                    case 3:
                        $sourcePath = imagerotate($sourcePath, 180, 0);
                        break;
                    case 6:
                        $sourcePath = imagerotate($sourcePath, -90, 0);
                        break;
                    case 8:
                        $sourcePath = imagerotate($sourcePath, 90, 0);
                        break;
                }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

当我正确看到它时$sourcePath是文件路径的变量,无法轮换...

请参阅http://php.net/manual/en/function.imagerotate.php,您必须传递图片的已打开资源。所以你必须做这样的事情

$oldImage = ImageCreateFromJPEG($sourcePath);
switch($orientation){
    case 3:
        $newImage = imagerotate($oldImage, 180, 0);
        break;
    case 6:
        $newImage = imagerotate($oldImage, -90, 0);
        break;
    case 8:
        $newImage = imagerotate($oldImage, 90, 0);
        break;
    default:
        $newImage = $oldImage;
}
imagejpeg($newImage, $targetPath, 90);