如何修复图像方向并使用PHP上传图像 [英] how to fix Orientation of the image and upload image using PHP

查看:130
本文介绍了如何修复图像方向并使用PHP上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传图像并修复图像的旋转,然后将其更新到服务器,我必须在此链接中使用答案:

I'm trying to upload an image and fix the rotate of the image, then update it to a server, I have to use the answer in this link: here but it does not work still the image with the wrong orientation

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 ",以修复浏览器中图像的方向,但是当我提交图像并将其上传到服务器时,其方向仍然相同

I used the image library "https://github.com/Al-Alloush/JavaScript-Load-Image", to fix the orientation of the image in browser but when I submit and uploaded the image to server still the same oriented

请帮助,:)

推荐答案

旋转图像后尚未保存图像.上载之前,请按以下步骤保存.

You haven't saved your image after rotating it. Save is as below before uploading it.

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.

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

这篇关于如何修复图像方向并使用PHP上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆