0

hey guy's i have a little issue...

i have an image up loader that works great on the PC, moves-resize-thumbnail all.

but.the issue is when i use the cell to upload it rotates the image 90 degrees..

i read that it is a common issue with phones...

So i need to make an editor to rotate the image by user..so if he uses the cell he edits the image to rotate correctly...

is it possible to does this?

i seem lots of image rotaters but when it uploads not in edit?

here is my code:

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


?>

I was able to fix my issue with a simple rotating script.i call it to rotate the pic and it save the name back in my database.

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

save stuff is here

techker
  • 189
  • 1
  • 10
  • Please show your code so folks can see if you are using **GD**, or **ImageMagick**, or **GraphicsMagick** or **php_vips** to rotate your images. – Mark Setchell Mar 30 '18 at 16:54
  • im not using anything to rotate the image. but i will edit.thx – techker Mar 30 '18 at 18:30
  • See http://us1.php.net/manual/en/imagick.getimageorientation.php. Unfortunately, I do not see an equivalent to the command line -auto-orient. So you will have to get the orientation and do a conditional to rotate the correct amount. – fmw42 Mar 30 '18 at 21:56

1 Answers1

0

Ok, the image functions you are using are from GD and described here.

The issue is the Orientation tag in the EXIF data which you are currently not respecting, so you need to read it, then act on it. There are several possibilities. Either follow the advice here, or change to ImageMagick instead of GD and use its orientation functions.

See also here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    whats odd is when i upload from files in my phone they are ok if i take a picture it uploads sideways....so confusing – techker Apr 01 '18 at 16:55