0

How can I get the width and height of the image after rotating it using imagerotate() in PHP?

Here is my code:

<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

// Content type
header('Content-type: image/jpeg');

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
imagejpeg($rotate);

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>

But what I want to do before doing the output, is that I want to get the width and height of the rotated image. How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
aslamdoctor
  • 3,753
  • 11
  • 53
  • 95

2 Answers2

1

I'm sure you can do something similar to this:

$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];

Another option is this:

list($width, $height) = getimagesize($filename);

Albzi
  • 15,431
  • 6
  • 46
  • 63
  • No, it doesn't work that way. I already tried that before posting here :) – aslamdoctor Sep 04 '13 at 11:25
  • 1
    So if it doesn't work, what does it do instead? Does it error, does it crash the server, does it return incorrect values? The width and height of an image shouldn't change if rotated 180 degrees – Mark Baker Sep 04 '13 at 11:27
  • Can't you use it before you rotate it getting the image's height and width? – Albzi Sep 04 '13 at 11:30
  • It says "Warning: getimagesize() expects parameter 1 to be string" Its clearly mentioned in php doc that it must pass the path to image, not the image object – aslamdoctor Sep 04 '13 at 11:30
  • @aslamdoctor - You know the value of `$filename` you use when you do `$source = imagecreatefromjpeg($filename);`... what do you get if you use that same `$filename` value in `$data = getimagesize($filename);` as per BeatAlex's suggestion? – Mark Baker Sep 04 '13 at 12:07
1

imagerotate returns an image ressource. Hence you cannot use getimagesize which works with an image file. Use

$width = imagesx($rotate);
$height = imagesy($rotate);

instead.

Kalhua
  • 559
  • 4
  • 2
  • This is the correct answer to the question. `getimagesize()` is for dimensions of the original file before rotation. `imagesx()` and `imagesy()` is for dimensions or the processed file after rotation. – OXiGEN Mar 15 '19 at 11:18