百科狗-知识改变命运!

imagerotate() - gd函数(图像处理)

梵高2年前 (2023-11-21)阅读数 21#技术干货
文章标签图像

imagerotate()

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

用给定角度旋转图像

说明

imagerotate(resource $image,float $angle,int $bgd_color[,int $ignore_transparent= 0]): resource

将$src_im图像用给定的$angle角度旋转。$bgd_color指定了旋转后没有覆盖到的部分的颜色。

imagerotate() - gd函数(图像处理)

旋转的中心是图像的中心,旋转后的图像会按比例缩小以适合目标图像的大小——边缘不会被剪去。

参数

$image

由图象创建函数(例如imagecreatetruecolor())返回的图象资源。

$angle

Rotation angle, in degrees. The rotation angle is interpreted as the number of degrees to rotate the image anticlockwise.

$bgd_color

Specifies the color of the uncovered zone after the rotation

$ignore_transparent

如果被设为非零值,则透明色会被忽略(否则会被保留)。

返回值

返回旋转后的图像资源,或者在失败时返回FALSE

更新日志

版本说明
5.1.0新增:$ignore_transparent。

范例

将图像旋转 180 度

本例将把一幅图像旋转 180 度——上下颠倒。


以上例程的输出类似于:

注释

Note:此函数仅在与 GD 库捆绑编译的 PHP 版本中可用。

After some INet searches and personal try-and-failures I succeed to rotate PNG images with preserving alpha channel transparency (semi transparency).

An issue with imagerotate() is it might need more memory than what is available. It was failing in my script so I tried to increase it with ini_set('memory_limit', '256M') 
and everything works fine now.
I've benchmarked three methods described here for rotating an image.
I used a 1600 x 1200 jpeg picture that was duplicated ten times on the disk. I looped through them to apply a CW rotation using either method.
First algorithm (the "imagesetpixel" algorithm):

Second algorithm (the "imagecopy" algorithm):

Third algorithm (the "imagerotate" algorithm):

In each step of the loop, the image is loaded, rotated then a copy is written on the disk.
Here are the results:
imagesetpixel algorithm: the 10 pictures were processed in 29 seconds
imagecopy algorithm: the 10 pictures were processed in 26 seconds
imagerotate algorithm: the 10 pictures were processed in 2 seconds (no typo here)
Writing the pictures on the disk allowed me to control the results. Every resulting picture was the same (tested with winmerge).
Conclusion: imagerotate is way faster than other "home-made" algorithms.
I ran the tests several times to see if the difference between imagesetpixel and imagecopy was introduced by my computer's independent activity, but I always got that 10% difference.
Note: the picture I used was taken from a camera, so no transparency was involved.
I ran the tests on Windows XP using PHP Version 5.3.0
Keep in mind this function rotates counterclockwise. if you want to go 90 degrees clockwise, you have to tell imagerotate to go 270 degrees counter-clockwise.
$angle = 360-$angle;

I was quite surprised to see that no one had done any working rotate function. And since I needed one for my web hotel, I whipped this one up. Someone might want to expand it so that it matches imagerotate.
Also, does imagerotate really rotate things counter clockwise? Otherwise this should also be changed.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}