PHP EXIF Orientation Rotate Passing New height and Width

This upload script handles image upload for TinyMCE 5 editor, when image has EXIF orientation meta data it rotates image on server side. The problem is TinyMCE editor then displays the image with wrong width and height in these cases, so I need to pass new height and width ? Can anyone suggest how to do do this please?

<?php
//error_reporting(0);
//Only these origins are allowed to upload images *
    $accepted_origins = array("http://localhost", "http://127.0.0.1", "http://192.168.1.1", "http://example.com");

    // Set the destination folder for image
    $month = strtolower(date('M'));
    $year = date('Y');
    $newspath = "img/news/";

    if (!is_dir("$newspath/$year/$month")) {
    mkdir("$newspath/$year/$month", 0777, true);
}
    $imageFolder = "$newspath/$year/$month/";

    reset ($_FILES);
    $temp = current($_FILES);
    if (is_uploaded_file($temp['tmp_name'])){
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            // same-origin requests won't set an origin. If the origin is set, it must be valid.
            if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
                header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
            } else {
                header("HTTP/1.1 403 Origin Denied");
                return;
            }
        }

        /*
            If your script needs to receive cookies, set images_upload_credentials : true in
            the configuration and enable the following two headers.
        */
        // header('Access-Control-Allow-Credentials: true');
        // header('P3P: CP="There is no P3P policy."');

        // Rewrite bad filenames
        $temp['name'] = preg_replace(array('/\s+/', '/[^a-zA-Z0-9\-\._]/', '/\.(?=.*\.)/', '/[\-]+/', '/[_]+/'), array('_', '', '_', '_', '_'), strtolower($temp['name']));

        // Verify extension
        if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "jpeg", "png"))) {

                header("HTTP/1.1 400 Invalid extension.");
                return;
        }

        // Accept upload if there was no origin, or if it is an accepted origin
        $tempname = $temp['name'];
        $filetowrite = $imageFolder . $tempname;
        $randompin = mt_rand(1000, 9999);
        // If file name already exists rename
        if (file_exists($filetowrite)) {
        $tempname =  $randompin . $tempname;
        $filetowrite = $imageFolder . $tempname;
        }

        // If EXIF Orientation data exists, rotate image as required
        $savetmpname = $temp['tmp_name'];
        function correctImageOrientation($savetmpname) {
            if (function_exists('exif_read_data')) {
                $exif = exif_read_data($savetmpname);
                if($exif && isset($exif['Orientation'])) {
                    $orientation = $exif['Orientation'];
                    if($orientation != 1){
                        $img = imagecreatefromjpeg($savetmpname);
                        $deg = 0;
                        switch ($orientation) {
                            case 3:
                                $deg = 180;
                                break;
                            case 6:
                                $deg = 270;
                                break;
                            case 8:
                                $deg = 90;
                                break;
                        }
                        if ($deg) {
                            $img = imagerotate($img, $deg, 0);       
                        }
                        //Rewrite rotated image back to the disk
                        imagejpeg($img, $savetmpname, 75);
                    } // if there is some rotation necessary
                } // if have the exif orientation info
            } // if function exists     
        }
        // Move uploaded files and rotate if required
        move_uploaded_file($savetmpname, $filetowrite);
        correctImageOrientation($filetowrite);
        //}
        // Respond to the successful upload with JSON.
        // Use a location key to specify the path to the saved image resource.
        // { location : '/your/uploaded/image/file'}
        // echo json_encode(array('location' => $filetowrite));

        $passpath = $year.'/'.$month.'/'.$tempname;

        echo json_encode(array('location' => $passpath));
    } else {
        // Notify editor that the upload failed
        header("HTTP/1.1 500 Server Error");
    }
?>

In this part of the code. As you know you’re rotating it here, have another switch() statement to deal with modifying the height and width parameters.

I get that just not sure how to do that

Don’t you just need to swap the height and width?

Yes I think that’s right, if the image is rotated 90 or 270 degress

I worked it out:

		// If EXIF Orientation data exists, rotate image as required
		$savetmpname = $temp['tmp_name'];
		function correctImageOrientation($savetmpname) {
			if (function_exists('exif_read_data')) {
				$exif = exif_read_data($savetmpname);
				if($exif && isset($exif['Orientation'])) {
					$orientation = $exif['Orientation'];
					if($orientation != 1){
						$img = imagecreatefromjpeg($savetmpname);
						$imgwidth = imagesx($img);
						$imgheight = imagesy($img);
						//$imgwidth = ImagesX($img);
						//$imgheight = ImagesY($img);
						$deg = 0;
						switch ($orientation) {
							case 3:
								$deg = 180;
								$newimgwidth = $imgwidth;
								$newimgheight = $imgheight;
								break;
							case 6:
								$deg = 270;
								$newimgwidth = $imgheight;
								$newimgheight = $imgwidth;
								break;
							case 8:
								$deg = 90;
								$newimgwidth = $imgheight;
								$newimgheight = $imgwidth;
								break;
						}
						if ($deg) {
							$img = imagerotate($img, $deg, 0);  
							$img = imagecopyresized($img, $savetmpname, 0, 0, 0, 0, $newimgwidth, $newimgheight, $imgwidth, $imgheight); 
						}
						// then rewrite the rotated image back to the disk as $filename
						imagejpeg($img, $savetmpname, 75);
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.