Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

image

php-gd

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)

like image 393
Ryan Griggs Avatar asked Jan 08 '18 20:01

Ryan Griggs


2 Answers

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;
like image 111
Anant Kumar Singh Avatar answered Dec 05 '22 14:12

Anant Kumar Singh


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

like image 36
Kwarcu Avatar answered Dec 05 '22 16:12

Kwarcu