php - 如何在使用 imagerotate() 旋转图像后获得新的宽度和高度?

标签 php

我如何真正获得图像旋转后设置的新宽度和高度?

$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;

我想要这个是因为我想根据上面创建的图像调整图像大小。 ($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);    

最佳答案

使用函数 imagesx()imagesy()使用 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");

关于php - 如何在使用 imagerotate() 旋转图像后获得新的宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28762132/

相关文章:

php在夏令时后显示错误的时间

php - 使用简单 XML 添加具有相同名称的子元素

php - 显示执行返回值

php - 带有基于 Linux 的 NAS (synology) 的家庭自动化 x10

php - 使用 Payum 定期付款的代币问题

php - 如何从另一个数组键的值创建一个数组?

php - Symfony2、 Twig 和 JavaScript

php - 在 PHP 中执行 Curl 以进行 Stripe 订阅

php - 从自定义自动建议自动完成创建链接

php - mysql - 子查询和连接