PHP图像旋转

时间:2018-03-30 15:56:11

标签: php image

嘿,伙计我有一点问题......

我有一个在PC上运行良好的图像加载程序,move-resize-thumbnail all。

但问题是,当我使用单元格上传时,它将图像旋转90度..

我读到这是电话的常见问题......

所以我需要制作一个编辑器来按用户旋转图像..如果他使用他编辑图像的单元格正确旋转...

可以这样做吗?

我似乎有很多图像旋转器,但是当它上传时不是编辑?

这是我的代码:

   session_start();
include_once 'config/dbconnect.php';


if (!isset($_SESSION['userSession'])) {
    header("Location: index.php");
}


if(isset($_POST["submit"])) {
    if(is_array($_FILES)) {


           $User=$_POST['User'];
           //$product_name=$_POST['Name'];
           $product_desc=$_POST['Desc'];
           $product_date_exp=$_POST['Exp'];
           $product_qty=$_POST['QTY'];
           $product_folder=$_POST['Folder'];
           $product_cat=$_POST['Cat'];
           $product_month=$_POST['Month'];

           $status ="Public";

            $product_code="1";
            $product_type="Coupons";

        $file = $_FILES['image']['tmp_name'];
        $sourceProperties = getimagesize($file);

        $fileNewNTime = time();
                $fileNewName =$User."_".$product_month."_".$fileNewNTime;

        $folderPath = "Folders/".$product_folder."/";
        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
        $imageType = $sourceProperties[2];







        switch ($imageType) {


            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file);
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);
                break;


            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file);
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagegif($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);
                break;


            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file);
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath. $fileNewName. "_thump.". $ext);
                break;


            default:
                echo "Invalid Image type.";
                exit;
                break;
        }






        move_uploaded_file($file, $folderPath. $fileNewName. ".". $ext);

        $Coupons =  $folderPath + "/"+  $fileNewName. ".". $ext;

        $CouponsNom =  $fileNewName. ".". $ext;

        $CouponThumb =$fileNewName. "_thump.". $ext;

        //$product_cat = $product_folder;

        //insert client
        mysqli_query($DBcon,"INSERT INTO Coupon_list (product_name,product_desc,product_code,product_date_exp,product_user,
                                                        product_image,product_image_thumb,product_status,product_qty,product_folder,product_cat,product_type,product_month)
        VALUES ('$fileNewName','$product_desc','$product_code','$product_date_exp','$User',
                 '$CouponsNom','$CouponThumb','$status','$product_qty','$product_folder','$product_cat','$product_type','$product_month')");

        //echo "Image Resize Successfully. to $folderPath";
            echo "<meta http-equiv=Refresh content=1;url=Upload.php?success=1>";

    }
}


function imageResize($imageResourceId,$width,$height) {


    $targetWidth =200;
    $targetHeight =200;


    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);


    return $targetLayer;
}


?>

我能用一个简单的旋转脚本解决我的问题。我调用它来旋转图片并将其保存在我的数据库中。

    $degrees = -270;
$path = $_GET['Folder'];
$file =$_GET['Pic'];
$fileid =$_GET['id'];

$image = $path.'/'.$file;
$imageN = $path.'/New_'.$file;
//load the image
$source = imagecreatefromjpeg($image);

//rotate the image
$rotate = imagerotate($source, $degrees, 0);

$NewImg='New_'.$file ;

//set the Content type
//header('Content-type: image/jpeg');

//display the rotated image on the browser
//imagejpeg($rotate);
imagejpeg($rotate,$imageN,100);
//free the memory
imagedestroy($source);
imagedestroy($rotate);

保存东西就在这里

1 个答案:

答案 0 :(得分:0)

好的,您使用的图像功能来自GD并描述了here

问题是您目前不尊重的EXIF数据中的Orientation标记,因此您需要阅读它,然后对其采取行动。有几种可能性。请遵循建议here,或更改为 ImageMagick 而不是GD并使用其orientation函数。

另见here