Rotate uploaded image according to Exif data and width/height

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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.