php - 为什么在 PHP 中调整上传图片大小时我的图片会旋转?

标签 php file upload rotation

<分区>

Possible Duplicate:
php resizing image on upload rotates the image when i don't want it to

我已经创建了我的第一个上传代码并正在测试它,图像调整大小并上传正常。我遇到的唯一问题是它会旋转。

代码如下:

$errors = array();
$message = "";

if(isset($_POST["test"])){

$name               = rand(1,999999) . $_FILES["uploadfile"]["name"];
$temp_name          = $_FILES["uploadfile"]["tmp_name"];
$size               = $_FILES["uploadfile"]["size"];
$extension          = strtolower(end(explode('.', $_FILES['uploadfile']['name'])));
$path               =   "testupload/" . $name;

$info               = getimagesize($temp_name);
$originalwidth      = $info[0];
$originalheight     = $info[1];
$mime               = $info["mime"];

$acceptedHeight     = 750;
$acceptedWidth      = 0;

$acceptedMimes = array('image/jpeg','image/png','image/gif');
$acceptedfileSize = 4102314;
$acceptedExtensions = array('jpg','jpeg','gif','png');
echo $size;
// check mimetype
if(!in_array($mime, $acceptedMimes)){$errors[] = "mime type not allowed - The file you have just uploaded was a: " . $extension . " file!";}
if(!$errors){if($size > $acceptedfileSize){$errors[] = "filesize is to big - Your file size of this file is: " . $size;}}
if(!$errors){if(!in_array($extension, $acceptedExtensions)){$errors[] = "File extension not allowed - The file you have just uploaded was a: " . $extension . " file!";}}

if(!$errors){

    // create the image from the temp file.
    if ($extension === 'png'){
        $orig = imagecreatefrompng($temp_name);
    }elseif ($extension === 'jpeg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'jpg'){
        $orig = imagecreatefromjpeg($temp_name);
    }elseif ($extension === 'gif'){
        $orig = imagecreatefromgif($temp_name);
    }

    // work out the new dimensions.
    if ($acceptedHeight === 0){
        $newWidth = $acceptedWidth;
        $newHeight = ($originalheight / $originalwidth) * $acceptedWidth;
    }else if ($acceptedWidth === 0){
        $newWidth = ($originalwidth / $originalheight) * $acceptedHeight;
        $newHeight = $acceptedHeight;
    }else{
        $newWidth = $acceptedWidth;
        $newHeight = $acceptedHeight;
    }

    $originalwidth = imagesx($orig);
    $originalheight = imagesy($orig);


    // make ssure they are valid.
    if ($newWidth  < 1){ $newWidth  = 1; }else{ $newWidth  = round($newWidth ); }
    if ($newHeight < 1){ $newHeight = 1; }else{ $newHeight = round($newHeight); }

    // don't bother copying the image if its alreay the right size.
    if ($originalwidth!== $newWidth || $originalheight !== $newHeight){

        // create a new image and copy the origional on to it at the new size.
        $new = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($new, $orig, 0,0,0,0, $newWidth, $newHeight, $originalwidth, $originalheight);

    }else{

        // phps copy on write means this won't cause any harm.
        $new = $orig;
    }

    // save the image.
    if ($extension === 'jpeg' || $extension === 'jpg'){
        imagejpeg($new, $path, 100);
    }else if ($save_ext === 'gif'){
        imagegif($new, $path);
    }else{
        imagepng($new, $path, round(9 - (100 / (100 / 9))));
    }

    $message = $path;

有人可以告诉我发生了什么事吗?

最佳答案

检查原始图像是否确实处于您期望的方向。我整天都在处理图像,大部分时间是 Windows 照片查看器以特定方向显示图像(读取 EXIF 中的方向变化),但如果你在 Photoshop 中打开它,情况就不一样了。

关于php - 为什么在 PHP 中调整上传图片大小时我的图片会旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11454394/

相关文章:

php - 正则表达式提取 2 个大括号之间的字符串

javascript - 使用下拉标签来验证其他下拉值

java - 核心java中的文件处理

python - 在 Python 中,我如何迭代一个迭代器,然后再迭代另一个?

javascript - 我怎样才能以相同的形式从具有相同名称的不同选择选项中获取一个值

c# - File.CreateText/File.AppendText 与 File.AppendAllText

css - 使用 Grails 的插件

ios - 从 swift ios 将图像上传到服务器时出现错误 406

java - 如何使用预签名 URL 将文件上传到 S3,而不将其加载到内存中

php - mySQL 缓存 - 内存还是硬盘?