16

I just loaded an image into memory via

$img = imagecreatefromjpeg($filename);

Then, I applied a rotation to the image:

$r_img = imagerotate($img, $angle, 0);

Per the docs, imagerotate() may change the dimensions of the image. An example is rotating a rectangular image by anything other than 180 degrees. The canvas will be expanded to accommodate the entire image, filling the empty areas with color "0" or black.

How can I get the new dimensions after rotation without writing the image out to a file? (getimagesize() requires a filename and doesn't appear to support a resource reference)

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58

2 Answers2

26

You can use imagesx() and imagesy()

imagesx-Returns the width of the given image resource.

imagesy-Returns the height of the given image resource.

$width  = imagesx($r_img);
$height = imagesy($r_img);
echo $width;
echo $height;
Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • I have a folder with 100's images, how to find Height and width programatically and output saved in Excel sheet? – Gem Apr 30 '19 at 08:04
  • 1
    @Gem you need to traverse each folder and then you need to use same code for each image, at the same time you need to add all necessary details to sheet also. Please go through recursive directory iteration in php – Alive to die - Anant Apr 30 '19 at 08:25
2

As far as I can see imagesx & imagesy should do the trick.

Kwarcu
  • 141
  • 1
  • 4