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

时间:2012-07-12 14:45:01

标签: php file upload rotation

  

可能重复:
  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;

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

3 个答案:

答案 0 :(得分:1)

检查原始图像实际上是您期望的方向。我整天处理图像,大部分时间是Windows Photo Viewer以特定方向显示图像(读取EXIF中的方向更改),但如果在Photoshop中打开它则不同。

答案 1 :(得分:0)

我用两张图片测试了你的代码:一张是横向(宽度&gt;高度),另一张是肖像(高度&gt;宽度)。代码有效。

问题似乎是@Tavocado所说的:较新的相机有一个传感器,用于在拍摄照片时检测相机的方向,并将这些信息存储在照片中。此外,较新的照片查看软件从照片中读取该信息并在显示图像之前旋转它。所以你总是以正确的方向看到图像(天空向上,向下看)。然而,PHP函数(以及世界其他地方)不使用该信息并按原样显示图像。这意味着您必须自己旋转肖像图像。

只需在浏览器中加载图片(拖动地址栏上的文件)即可看到图片实际存储在文件中的方式,而无需自动旋转。

答案 2 :(得分:0)

问题是图片中嵌入了EXIF数据,可能来自拍摄照片的设备。

使用exif_read_data调查您的图片是否嵌入了轮换信息,然后使用function like this自动更正轮换。