Rotate uploaded image according to Exif data and width/height

General Tech Bugs & Fixes 3 years ago

8.38K 1 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

I've been trying to put some code together but my PHP isn't great. I've read a few similar solutions here but I'm not able to get it all working together.

I am trying to take an uploaded image (from a phone), rotate it according to the exif data, save it to server. Then use the same image, check to see if it is landscape and rotate to portrait. Then scale to 1200px wide x 1800px high. Then save to another folder.

    php
    function image_fix_orientation($filename) {
    $exif = exif_read_data($filename);
    if (!empty($exif['Orientation'])) {
        $image = imagecreatefromjpeg($filename);
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($image, 180, 0);
                break;

            case 6:
                $image = imagerotate($image, 90, 0);
                break;

            case 8:
                $image = imagerotate($image, -90, 0);
                break;
        }

        imagejpeg($image, $filename, 90);
    }
}


    $currentDir = getcwd();
    $uploadDirectory = "/uploads/";
    $printDirectory = "/prints/";

    $errors = []; // Store all foreseen and unforseen errors here

    $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions

    $fileName = $_FILES['myfile']['name'];
    $fileSize = $_FILES['myfile']['size'];
    $fileTmpName  = $_FILES['myfile']['tmp_name'];
    $fileType = $_FILES['myfile']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));

    $uploadPath = $currentDir . $uploadDirectory . basename($fileName);   
    $printPath = $currentDir . $uploadDirectory . $printDirectory . basename($fileName);   

    if (isset($_POST['submit'])) {

        $filename = $_FILES['myfile']['name'];
        $filePath = $_FILES['myfile']['tmp_name'];

        //get the image sizes
        $sizes = getimagesize($filePath);

        $width=$sizes[0];
        $height=$sizes[1];

        $source 
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum