PHP Imagick - output resized image AND its dimensions

282 views Asked by At

I have the following php and imagick script to resize an image dynamically:

resize.php

$_size = $_GET['size'];
$_path = $_GET['path'];
$img = new Imagick($_path);
$img_d = $img->getImageGeometry(); 
$img_w = $img_d['width']; 
$img_h = $img_d['height'];

..// more variables

$crop_w = round($_size * $p_w);
$crop_h = round($_size * $p_h);
list($_w, $_h) = scaleProportions($crop_w, $crop_h, $img_w, $img_h);
$img->resizeImage($_w, $_h, imagick::FILTER_LANCZOS, 0);

header("Content-Type: image/jpg");
header("Cache-control: Public"); 
$headerTimeOffset = 60 * 60 * 24 * 30; 
$headeExpire = "Expires: ".gmdate("D, d M Y H:i:s",time()+$headerTimeOffset)."    GMT"; 
header ($headeExpire);

$img->setImageFormat('jpg');
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality($_quality);

echo $img->getImageBlob();

And i call this script in my img tags like so:

images.html

<img src="http:www.mysite.com/resize/size/path" alt="xxxx">

This works as intended. I am now in need of not only getting the newly resized image but also it's new resized dimensions. Is this in anyway possible?

The reason for doing so is I am using the masonry plugin alongside lazy load on each image and want to set the grid before they have loaded. i know that masonry needs these sizes before the image has loaded

1

There are 1 answers

0
venca On

Little inefficient way is:

list($width, $height) = getimagesize("http://www.example.com/resize/size/path");

Or save resized image to disk, somewhere under document root, get weblink for src and path for getimagesize.