2012-07-12 54 views
3

可能重复:
php resizing image on upload rotates the image when i don't want it to为什么我的图像在PHP中调整上传图像大小时会旋转?

我已经创建了我的第一次上传代码,并测试它,图像大小调整和上传的罚款。我唯一的问题是它旋转。

下面是代码:

$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; 

可有人请让我知道是怎么回事?

+0

它接缝是所有高度大于宽度的图像,它旋转图像,我如何防止这种情况发生? – Robert 2012-07-12 15:01:49

+0

我在代码中看不到任何明显的东西。在每一步'var_dump(getimagesize($ new_image))'之后,找出它正在改变的位置。 – tavocado 2012-07-12 15:20:29

回答

1

检查原始图像实际上是您期望的方向。我整天使用图像,大部分时间是Windows照片浏览器以某种方向显示图像(读取EXIF中的方向更改),但如果在Photoshop中打开它,则会有所不同。

+0

上传的图片原本是另一种方式,现在我测试了几张图片,所有高度大于宽度的图片都旋转。 – Robert 2012-07-12 14:57:38

0

我用两个图像测试了你的代码:一个是横向(宽度>高度),另一个是纵向(高度>宽度)。代码起作用。

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

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

0

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

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