图像旋转后获取缩小图像的宽度和高度?

Get width and height of scaled down image after imagerotation?

这个问题的跟进:

我得到的答案是基于文件名的实际图像大小,但如果我想从另一个宽度和高度开始。

我将如何实现?请参阅下面的代码以了解我的尝试...

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

//Example for clarification (this is parameters that is used to save new image)
$ps['product_width'] = 40;      //for example $source_width = 200
$ps['product_height'] = 80;     //and $source_height = 400


$angle = $ps['product_angle'];
if (intval($angle) <> 0) {

   //Actual dimensions of image from filename
    $current_source_x = imagesx($source_image); 
    $current_source_y = imagesy($source_image);

    //Get current ratio of "scaled down image"
    $ratio_x = $ps['product_width'] / $current_source_x;
    $ratio_y = $ps['product_height'] / $current_source_y;

    $source_image = imagerotate( 
        $source_image, 
        360-$angle, 
        imageColorAllocateAlpha($source_image, 255, 255, 255, 127)
    );

   //New dimensions from actual filename
   //This would be fine if I just wanted the new width and height
   //based on the filenames dimension, but I want new dimensions
   //for the "scaled downed version" of the image (40, 80)
   $new_image_source_x = imagesx($source_image);
   $new_image_source_y = imagesy($source_image);

   //I tried this, but obviously I'm doing something totally wrong        
   $new_width = ($new_image_source_x - $current_source_x)  * $ratio_x;
   $new_height = ($new_image_source_y - $current_source_y)  * $ratio_y;

   //Set new width after rotation                                                    
   $ps['product_width']  = $new_width;
   $ps['product_height'] = $new_height;


}

$ps['source_image'] = $source_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
//
imagecopyresized($dest_image, $ps['source_image'], 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);    

可能我遗漏了一些非常重要的东西....

更新

实际值的示例...

image current width63.224619257754
image current height80.210337864315

//after calculation
image new width37.583523669887
image newt height21.716336015666

where angle is 41.10419020401479

在看我的评论

"I've actually came into a solution"

..那不是我发挥我最好的英语能力的一天...

我猜你知道我的意思,下面是我解决问题的方法:

在我的方法中,我试图根据 "scaled down image" 的比率计算新值,然后根据 "scaled down image" 和 "the image after rotated" 的差异获得新的宽度和高度.

像这样:

  1. 在 "scaled down image" 和原始图像之间设置 relation/ratio。
  2. 进行实际的图像旋转
  3. 获取旋转图像的原始尺寸之间的差异,并将其乘以图像旋转前设置的比率因子。
  4. 根据旋转角度得到新的宽高

这确实无法正常工作(在 step4 处失败)。我一直在四处寻找如何在旋转图像后计算宽度和高度的答案。但是这些宽度和高度与 GD 函数 imagesx()imagesy() return 在图像旋转后的尺寸不同。我已经使用 sin()cos() 尝试了几次计算来检索宽度和高度,但仍然没有得到与 imagesx()imagesy() 完全相同的值。

这让我开始思考...如果我将方法更改为:

  1. 在 "scaled down image" 和原始图像之间设置 relation/ratio。
  2. 进行实际的图像旋转
  3. 根据乘以 imagesx()imagesy() 的比率应用新尺寸 - 旋转后的值 return

新密码:

//Rotate resized image (if it should be)
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {

    //Get current dimensions from file
    $current_source_x = imagesx($source_image);
    $current_source_y = imagesy($source_image);

    //Get current ratio of "scaled down image"
    $ratio_x = $ps['product_width'] / $current_source_x;
    $ratio_y = $ps['product_height'] / $current_source_y;

    //Rotate image
    $source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));    

    //Now we get a new width from the imagerotate()-function, use those to set new_width from
    //ratio/propoprtions is used from origin width and height
    $ps['product_width']  = imagesx($source_image) * $ratio_x;
    $ps['product_height'] = imagesy($source_image) * $ratio_y;

}

这工作正常 - 几乎......现在的问题是如果旋转图像的新宽度 and/or 高度变得比图像的原始尺寸大,那么比例将不准确(并且有时会切断图像(取决于旋转角度))。

代码已修改,因此在创建调整大小的图像时,高度和宽度将与文件中的图像成比例。

//Rotate resized image (if it should be)
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {

    //Get current dimensions from file
    $current_source_x = imagesx($source_image);
    $current_source_y = imagesy($source_image);

    //Get current ratio of "scaled down image"
    $ratio_x = $ps['product_width'] / $current_source_x;
    $ratio_y = $ps['product_height'] / $current_source_y;

    //Rotate image
    $source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));    

    //Now we get a new width from the imagerotate()-function, use those to set new_width from
    //ratio/propoprtions is used from origin width and height
    $ps['product_width']  = imagesx($source_image) * $ratio_x;
    $ps['product_height'] = imagesy($source_image) * $ratio_y;

    //Set these so we can modifiy the width and height given from getimagesize()-function below
    $ps['source_width'] = imagesx($source_image) ;
    $ps['source_height'] =  imagesy($source_image);            
}


//If image is rotated, then width and height are adjusted with these values
if (isset($ps['source_width']) && isset($ps['source_height']) ) {
    $source_width = $ps['source_width'];
    $source_height = $ps['source_height'];                                    
}

//Set position where to place in the image to save
$dest_x = $ps['product_left'];
$dest_y = $ps['product_top'];

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

//Resize source-image to new width and height and then copy from source to destination point
imagecopyresized($dest_image, $ps['source_image'], $dest_x, $dest_y, 0, 0, $dest_width, $dest_height, $source_width, $source_height);  

所以我的最终解决方案将包括以下步骤:

  1. 在 "scaled down image" 和原始图像之间设置 relation/ratio。
  2. 进行实际的图像旋转
  3. 根据乘以 imagesx()imagesy() 的比率应用新尺寸 - 旋转后的值 return
  4. 在图像旋转时设置"fake"宽度和高度,以便调整大小与原始图像成比例。

我希望这能帮助那些正在努力解决我一直在努力解决的相同问题(几个小时到很多)的人!