此问题的后续问题:如何在使用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
查看我的评论
"我实际上已经找到了一个解决方案"
这不是我运用我最好的英语技能的日子。。。
我想你知道我的意思,下面是我解决问题的方法:
在我的方法中,我试图根据"缩小图像"的比率计算新值,然后根据"缩小的图像"answers"旋转后的图像"的差异获得新的宽度和高度。
类似这样的东西:
- 设置"缩小图像"与原始图像之间的关系/比率
- 进行实际的图像旋转
- 从旋转后的图像中获取原始尺寸之间的差异,并将其与图像旋转前设置的比例因子相乘
- 根据旋转角度获取新的宽度和高度
这确实不正确(在步骤4失败)。我一直在四处寻找如何在图像旋转后计算宽度和高度的答案。但这些宽度和高度并没有返回与GD
-函数imagesx()
和imagesy()
在图像旋转后返回的尺寸相同的尺寸。我已经尝试了使用sin()
和cos()
来检索宽度和高度的几种计算,但仍然没有得到与imagesx()
和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()
和imagesy()
相乘的比率应用新尺寸标注-旋转后返回的值 - 当图像旋转时,设置"fake"宽度和高度,这样调整大小将与原始图像成比例
我希望这能帮助任何正在与我争论过的问题(几个小时到很多小时)作斗争的人!