php - 如何修复图像的方向并使用 PHP 上传图像

标签 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

请帮忙,:)

最佳答案

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

imagejpeg($image, $filePath);

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

Note: imagejpeg should be used for images with JPG extension. For PNG, use imagepng.

关于php - 如何修复图像的方向并使用 PHP 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48467336/

相关文章:

javascript - 从 Cropper 保存圆形裁剪图像

jquery - 我如何通过 jquery 和 ajax 替换图像(在 div 内)

arrays - 你能在核心数据中保存一组图像吗?

image - S3/DXT/BC 压缩和纹理格式

php - 数据库保存和检索最后保存的值

php - 发布多个同名字段

php - DateTime 使用不正确的时区

html - 居中图像并保持相同的高度

php-登录验证不起作用

php - symfony 2 中的自定义存储库类不起作用