Louis Tran Louis Tran - 1 year ago 80
PHP Question

Width and Height values are automatically swapped after uploading to server. [PHP]

I used this function

to get width and height of the image I uploaded.
The function returned a 2D array:
$width = arr[0]
$height = arr[1]

enter image description here

On my PC, the image demension is: 3024 x 4032 (w x h)
But on the server side, the image width and height values are swapped. 4032 x 3024 (w x h)

var_dump($width); // C:\wamp64\www\upload_script\Image.php:89:int 4032
var_dump($height); // C:\wamp64\www\upload_script\Image.php:90:int 3024

I'm not sure what caused this and how can I make it consistent. I appreciate any help.

Answer Source

You can check first comment on exif_read_data function in php manual.

Code copied from there:

$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
        case 3:
            $image = imagerotate($image,180,0);
        case 6:
            $image = imagerotate($image,-90,0);
// $image now contains a resource with the image oriented correctly
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download