此问题的后续内容: How to get new width and height after an image has been rotated with imagerotate()?
我得到的答案是基于文件名的实际图像大小,但是如果我想从另一个宽度和高度开始。
我将如何实现这一目标?请参阅以下代码以了解我的尝试......
$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
答案 0 :(得分:1)
查看我的评论
&#34;我实际上已经找到了解决方案&#34;
..那天我没有用我最好的英语技能......
我想你知道我的意思,这就是我解决问题的方法:
在我的方法中,我试图根据&#34;缩小图像&#34;的比率来计算新值。然后根据&#34;缩小图像&#34;的差异获得新的宽度和高度。和&#34;旋转后的图像&#34;。
这样的事情:
这确实无法正常工作( step4 失败)。我一直在四处寻找,以寻找在旋转图像后如何计算宽度和高度的答案。但是这些宽度和高度没有返回与图像旋转后GD
- 函数imagesx()
和imagesy()
返回的相同维度。我已使用sin()
和cos()
尝试了几项计算来检索宽度和高度,但仍未获得与imagesx()
和{{1}完全相同的值}。
这让我思考......如果我将方法改为:
imagesy()
和imagesx()
来应用新尺寸 - 旋转后返回值新代码:
imagesy()
这很好 - 几乎......现在的问题是,如果旋转后的图像的新宽度和/或高度比图像的原始尺寸大,那么比例就不会是准确(并会切断图像时间(取决于旋转角度))。
修改了代码,因此在创建调整大小的图像时,高度和宽度将与文件中的图像成比例。
//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;
}
所以我的最终解决方案将包括以下步骤:
//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);
和imagesx()
来应用新尺寸 - 旋转后返回值我希望这可以帮助那些在我遇到的同样问题上挣扎的人(几个小时到很多)!