PHP GD: How to get dimensions of image resource in memory without writing to file? [duplicate]
Asked Answered
C

2

17

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)

Carley answered 8/1, 2018 at 20:51 Comment(1)
imagesx: Returns the width of the given image resource. ?Charmainecharmane
F
29

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;
Fenugreek answered 8/1, 2018 at 20:56 Comment(2)
I have a folder with 100's images, how to find Height and width programatically and output saved in Excel sheet?Bartley
@Bartley 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 phpFenugreek
H
2

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

Hector answered 8/1, 2018 at 20:55 Comment(1)
Edit your answer to add sample code to increase the chances to get upvoted, and links to imagesx and imagesy documentation as a complement.Carmina

© 2022 - 2024 — McMap. All rights reserved.