Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get new width and height after an image has been rotated with imagerotate()?

Tags:

php

How do I actually get the new width and height that is set after image is rotated?

$ps['product_angle'] = 77; //Could be any angle
$filename = 'test.png'     //filename to the original product

list($source_width, $source_height) = getimagesize($filename);
$source_image = imagecreatefromjpeg($filename);         
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {
    $source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));
}

$ps['source_image'] = $source_image;

I want this because I want to do an image resize based on above image created. ($ps['source_image'])

//If I do an image 
list($source_width, $source_height) =     getimagesize($filename);

$dest_width = (int)$ps['product_width'];
$dest_height = (int)$ps['product_height'];

//Resize source-image to new width and height
//But this width and height are incorrect because they are 
//set before image is rotated and often the image is just "cut off" 
//
imagecopyresized($dest_image, $ps['source_image'], 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);    
like image 459
bestprogrammerintheworld Avatar asked Feb 27 '15 09:02

bestprogrammerintheworld


1 Answers

Use functions imagesx() and imagesy() to get the width and the height of an image loaded or created in memory using GD.

$filepath = '/tmp/1.jpg';
$size = getimagesize($filepath);
echo('Image dimensions returned by getimagesize()   : '.$size[0].'x'.$size[1]." pixels\n");

$img  = imagecreatefromjpeg($filepath);
$width  = imagesx($img);
$height = imagesy($img);
echo('Dimensions returned by imagesx() and imagesy(): '.$width.'x'.$height." pixels.\n");

$angle = 60;
$dst = imagerotate($src, $angle, imageColorAllocateAlpha($src, 255, 255, 255, 127));
$width  = imagesx($dst);
$height = imagesy($dst);
echo('Dimensions of the rotated image: '.$width.'x'.$height." pixels.\n");
like image 83
axiac Avatar answered Oct 16 '22 17:10

axiac