上传PHP之前无法旋转图像

时间:2016-06-02 04:51:38

标签: php

我想在上传之前自动旋转图片。买我的代码不旋转图像。

我尝试使用此图片上传,但我始终以原始图像方向输出。

Example Images

我的代码:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="fileUpload">
    <button type="submit" name="upload">Upload</button>
</form>

<?
if(isset($_FILES['fileUpload'])) {
    $jpgFile = $_FILES['fileUpload']['tmp_name'];
    $jpgFileName = $_FILES['fileUpload']['name'];
    $width = 640;
    $exif = exif_read_data($jpgFile, 0, true);
    $orientation = $exif['Orientation'];
    $file_ext = substr($jpgFileName, strripos($jpgFileName, '.')); // get file name
    $FileName = "Rotate_Complete";
    $thumbFile = "images/".$FileName.$file_ext;
    function rotate($jpgFile, $thumbFile, $width, $orientation) {
        // Get new dimensions
        list($width_orig, $height_orig) = getimagesize($jpgFile);
        $height = (int) (($width / $width_orig) * $height_orig);
        // Resample
        $image_p = imagecreatetruecolor($width, $height);
        $image   = imagecreatefromjpeg($jpgFile);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
        // Fix Orientation
        if(!empty($orientation)) {
            switch($orientation) {
                case 3:
                    $image_p = imagerotate($image_p, 180, 0);
                    break;
                case 6:
                    $image_p = imagerotate($image_p, -90, 0);
                    break;
                case 8:
                    $image_p = imagerotate($image_p, 90, 0);
                    break;
            }
        }
        // Output
        if(imagejpeg($image_p, $thumbFile, 90)) {
            echo "Complete";
        } else {
            echo "Error!";
        }
    }
    rotate($jpgFile, $thumbFile, $width, $orientation);
}
?>

提前致谢。

0 个答案:

没有答案