如何使用PHP修复图像的方向和上传图像

时间:2018-01-26 18:23:12

标签: php image

我尝试上传图片并修复图片的旋转,然后将其更新到服务器,我必须使用此链接中的答案:here 但它仍然无法正确处理错误方向的图像

php中的代码:

    //START Update the user image
if(isset($_POST['user_image_id'])){

    $upload_image_id = $_POST['user_image_id'];
    $user_image = $_FILES['user_image']['name'];
    $filePath = $_FILES['user_image']['tmp_name'];

   $image = imagecreatefromstring(file_get_contents($_FILES['user_image']['tmp_name']));
    $exif = exif_read_data($filePath);
    if(!empty($exif['Orientation'])) {
        switch($exif['Orientation']) {
           case 8:
               $image = imagerotate($image,90,0);
               break;
           case 3:
               $image = imagerotate($image,180,0);
               break;
           case 6:
               $image = imagerotate($image,-90,0);
               break;
       }
   }
    // $image now contains a resource with the image oriented correctly

    //create image target to upload
    $image_target = $folder_target.basename($_FILES['user_image']['name']);

    //upload the image the and update the name of the image
    if(move_uploaded_file($_FILES['user_image']['tmp_name'], $image_target)){
        if ($conn->query("UPDATE `users` SET `image`='$user_image' WHERE `id` LIKE '$upload_image_id'")){
            echo 1;
        }else{
            echo "not update the name, Proplem";
        }
    }else{
        echo 'image not uploaded';
    }

}

我使用了图像库" https://github.com/Al-Alloush/JavaScript-Load-Image",来修复浏览器中图像的方向,但是当我提交并上传图像到服务器仍然是相同的导向

before uploading image display like that after uploading to the server

请帮助,:)

1 个答案:

答案 0 :(得分:3)

旋转后您还没有保存图像。在上传之前保存如下。

imagejpeg($image, $filePath);

//upload the image the and update the name of the image
if(move_uploaded_file($filePath, $image_target)){
    ....
}
  

注意:imagejpeg应该用于JPG扩展名的图片。对于PNG,请使用imagepng