How on php , you can get the orientation of the image (for example: the orientation of the image is horizontal, vertical, square)?

enter image description here

    2 answers 2

    Well, for this you first need to get the width and height of the image using the function getimagesize () , and then compare the width and height.

     $foto = "hummer.jpg"; echo '<img src = "'.$foto.'" />'; echo "<br>"; // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ массив, содСрТащий Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΈ Ρ‚ΠΈΠΏ изобраТСния $size = getimagesize ("$foto"); $flag = array( 1=>'GIF', 2=>'JPG', 3=>'PNG', 4=>'SWF', 5=>'PSD', 6=>'BMP', 7=>'TIFF(Π±Π°ΠΉΡ‚ΠΎΠ²Ρ‹ΠΉ порядок intel)', 8=>'TIFF(Π±Π°ΠΉΡ‚ΠΎΠ²Ρ‹ΠΉ порядок motorola)', 9=>'JPC', 10=>'JP2', 11=>'JPX' ); /// == ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅ΠΌ высоту ΠΈ ΡˆΠΈΡ€ΠΈΠ½Ρƒ Ρ„ΠΎΡ‚ΠΎ == /// $height_img = $size[0]; // Высота ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $width_img = $size[1]; // Π¨ΠΈΡ€ΠΈΠ½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ if ($height_img == $width_img) {echo "ΠšΠ²Π°Π΄Ρ€Π°Ρ‚Π½Π°Ρ";} elseif ($height_img > $width_img) {echo "Π“ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΡŒΠ½Π°Ρ";} else {echo "Π’Π΅Ρ€Ρ‚ΠΈΠΊΠ°Π»ΡŒΠ½Π°Ρ";}; /// == Для ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Ρ‡Ρ‚ΠΎ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ функция getimagesize() == /// echo "<br><br>"; echo "Π¨ΠΈΡ€ΠΈΠ½Π°: " . $size[0] .'<br>'; echo "Высота: " . $size[1] .'<br>'; echo "Π’ΠΈΠΏ изобраТСния: " . $flag[$size[2]] .'<br>'; echo "Π¨ΠΈΡ€ΠΈΠ½Π° ΠΈ Высота: " . $size[3] .'<br>'; 

      The orientation and dimension of the sides are not always related, if that ... If there is EXIF ​​data in the picture, you can try to determine from them:

       $file = 'path_to/image.jpg'; $exif = exif_read_data($file); if (!empty($exif['Orientation'])) { 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; } }