【问题标题】:Why is my image rotating when resizing uploaded image in PHP? [duplicate]为什么在 PHP 中调整上传图像的大小时我的图像会旋转? [复制]
【发布时间】:2012-07-12 08:30:17
【问题描述】:

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

谁能告诉我这是怎么回事?

【问题讨论】:

  • 它接缝是所有高度大于宽度的图像,它旋转图像,我如何防止这种情况发生?
  • 我在你的代码中看不到任何明显的东西。在每一步之后var_dump(getimagesize($new_image)) 计算出它在哪一点发生变化

标签: php file upload rotation


【解决方案1】:

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

【讨论】:

  • 上传的图片原来是反方向的,我现在测试了几张图片,所有高度大于宽度的图片都旋转了。
【解决方案2】:

我用两张图片测试了您的代码:一张是横向(宽度 > 高度),另一张是纵向(高度 > 宽度)。代码有效。

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

只需在浏览器中加载您的图像(将文件拖到地址栏上),您就会看到图像是如何真正存储在文件中的,无需任何自动旋转。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2013-09-19
      • 2021-11-05
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多