调用未定义的函数 imagerotate()

发布于 2024-10-26 19:16:40 字数 950 浏览 1 评论 0原文

我想我这里有一个奇怪的错误。

致命错误:调用未定义的函数 imagerotate() /var/www/web/html/include/php/class/image.class.php 第 30 行

第 30 行:

$im = imagerotate( $this->res, $degrees, $bkg );

根据 PHP 文档imagerotate() 应该是 PHP 4 以来的一个函数,而我使用的是 PHP 5。

为什么它不起作用?其他图像函数,例如 imagecreatefromjpeg()imagejpeg() 工作正常。

来自 phpinfo() 的 GD 信息:

GD 支持已启用
GD 版本 2.0 或更高版本
FreeType 支持已启用
FreeType 链接与 freetype
FreeType 版本2.3.7
T1Lib 支持已启用
GIF 读取支持已启用
GIF 创建支持已启用
JPG 支持已启用
PNG 支持已启用
WBMP 支持已启用

I have a strange error here, I think.

Fatal error: Call to undefined function imagerotate() in
/var/www/web/html/include/php/class/image.class.php
on line 30

LINE 30:

$im = imagerotate( $this->res, $degrees, $bkg );

According to the PHP documentation, imagerotate() should be a function since PHP 4 and I am using PHP 5.

Why does it not work? Other image functions, such as imagecreatefromjpeg() and imagejpeg() work fine.

GD info from phpinfo():

GD Support enabled
GD Version 2.0 or higher
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.7
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱本泡沫多脆弱 2024-11-02 19:16:40

页面中,您链接到它清楚地说

注意:此功能仅可用
如果 PHP 是用捆绑包编译的
GD 库的版本。

其中一条评论

这个函数显然包含一个
内存泄漏。正因为如此,才被
被排除在 GD 库之外
与Ubuntu(我假设其他操作系统,
也)。

所以,如果你运行的是 Ubuntu 并且
想知道为什么你会收到“调用未定义的
函数 imagerotate()" 即使
你似乎有正确的 GD 库
安装了,这就是原因。使用
由 beau 提供的替代方案
改为 Dragonflydevelopment . com。
它工作完美(对于角度
90 度步长)。

顺便说一下,其他一些 GD 图像
功能(不幸的是,越多
有趣的)遭受同样的痛苦
问题并且被排除在 Ubuntu 之外
分布也是如此。

如需可能的解决方案,请查看此处

http://www.php.net/manual/en/function.imagerotate.php#93151 并修改

if (!function_exists('imagerotate')) {

    /*
        Imagerotate replacement. ignore_transparent is work for png images
        Also, have some standard functions for 90, 180 and 270 degrees.
        Rotation is clockwise
    */

    function imagerotate_rotateX($x, $y, $theta) {
        return $x * cos($theta) - $y * sin($theta);
    }

    function imagerotate_rotateY($x, $y, $theta) {
        return $x * sin($theta) + $y * cos($theta);
    }

    function imagerotate($srcImg, $angle, $bgcolor = 0, $ignore_transparent = 0) {
        $srcw = imagesx($srcImg);
        $srch = imagesy($srcImg);

        //Normalize angle
        $angle %= 360;
        //Set rotate to clockwise
        $angle = -$angle;

        if ($angle == 0) {
            if ($ignore_transparent == 0) {
                imagesavealpha($srcImg, true);
            }
            return $srcImg;
        }

        // Convert the angle to radians
        $theta = deg2rad($angle);

        //Standart case of rotate
        if ((abs($angle) == 90) || (abs($angle) == 270)) {
            $width = $srch;
            $height = $srcw;
            if (($angle == 90) || ($angle == -270)) {
                $minX = 0;
                $maxX = $width;
                $minY = -$height+1;
                $maxY = 1;
            } else if (($angle == -90) || ($angle == 270)) {
                $minX = -$width+1;
                $maxX = 1;
                $minY = 0;
                $maxY = $height;
            }
        } else if (abs($angle) === 180) {
            $width = $srcw;
            $height = $srch;
            $minX = -$width+1;
            $maxX = 1;
            $minY = -$height+1;
            $maxY = 1;
        } else {
            // Calculate the width of the destination image.
            $temp = array(
                imagerotate_rotateX(0, 0, 0 - $theta),
                imagerotate_rotateX($srcw, 0, 0 - $theta),
                imagerotate_rotateX(0, $srch, 0 - $theta),
                imagerotate_rotateX($srcw, $srch, 0 - $theta)
            );
            $minX = floor(min($temp));
            $maxX = ceil(max($temp));
            $width = $maxX - $minX;

            // Calculate the height of the destination image.
            $temp = array(
                imagerotate_rotateY(0, 0, 0 - $theta),
                imagerotate_rotateY($srcw, 0, 0 - $theta),
                imagerotate_rotateY(0, $srch, 0 - $theta),
                imagerotate_rotateY($srcw, $srch, 0 - $theta)
            );
            $minY = floor(min($temp));
            $maxY = ceil(max($temp));
            $height = $maxY - $minY;
        }

        $destimg = imagecreatetruecolor($width, $height);
        if ($ignore_transparent == 0) {
            imagefill($destimg, 0, 0, imagecolorallocatealpha($destimg, 255,255, 255, 127));
            imagesavealpha($destimg, true);
        }

        // sets all pixels in the new image
        for ($x = $minX; $x < $maxX; $x++) {
            for ($y = $minY; $y < $maxY; $y++) {
                // fetch corresponding pixel from the source image
                $srcX = round(imagerotate_rotateX($x, $y, $theta));
                $srcY = round(imagerotate_rotateY($x, $y, $theta));
                if ($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch) {
                    $color = imagecolorat($srcImg, $srcX, $srcY);
                } else {
                    $color = $bgcolor;
                }
                imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
            }
        }

        return $destimg;
    }

}

In the page you linked to it clearly says

Note: This function is only available
if PHP is compiled with the bundled
version of the GD library.

And one of the comments say

This function apparently contains a
memory leak. Because of this, it was
kept out of the GD library that comes
with Ubuntu (and I assume other OS'es,
too).

So, If you are running Ubuntu and
wonder why you get "Call to undefined
function imagerotate()" even though
you seemingly have the correct GD lib
installed, this is why. Use the
alternative supplied by beau at
dragonflydevelopment dot com instead.
It works flawlessly (for angles in
steps of 90 degrees).

On a side-note, some other GD image
functions (unfortunately, the more
interesting ones) suffer from the same
problem and are kept out of the Ubuntu
distribution as well.

For a possible solution look here

Code copied from http://www.php.net/manual/en/function.imagerotate.php#93151 and revised

if (!function_exists('imagerotate')) {

    /*
        Imagerotate replacement. ignore_transparent is work for png images
        Also, have some standard functions for 90, 180 and 270 degrees.
        Rotation is clockwise
    */

    function imagerotate_rotateX($x, $y, $theta) {
        return $x * cos($theta) - $y * sin($theta);
    }

    function imagerotate_rotateY($x, $y, $theta) {
        return $x * sin($theta) + $y * cos($theta);
    }

    function imagerotate($srcImg, $angle, $bgcolor = 0, $ignore_transparent = 0) {
        $srcw = imagesx($srcImg);
        $srch = imagesy($srcImg);

        //Normalize angle
        $angle %= 360;
        //Set rotate to clockwise
        $angle = -$angle;

        if ($angle == 0) {
            if ($ignore_transparent == 0) {
                imagesavealpha($srcImg, true);
            }
            return $srcImg;
        }

        // Convert the angle to radians
        $theta = deg2rad($angle);

        //Standart case of rotate
        if ((abs($angle) == 90) || (abs($angle) == 270)) {
            $width = $srch;
            $height = $srcw;
            if (($angle == 90) || ($angle == -270)) {
                $minX = 0;
                $maxX = $width;
                $minY = -$height+1;
                $maxY = 1;
            } else if (($angle == -90) || ($angle == 270)) {
                $minX = -$width+1;
                $maxX = 1;
                $minY = 0;
                $maxY = $height;
            }
        } else if (abs($angle) === 180) {
            $width = $srcw;
            $height = $srch;
            $minX = -$width+1;
            $maxX = 1;
            $minY = -$height+1;
            $maxY = 1;
        } else {
            // Calculate the width of the destination image.
            $temp = array(
                imagerotate_rotateX(0, 0, 0 - $theta),
                imagerotate_rotateX($srcw, 0, 0 - $theta),
                imagerotate_rotateX(0, $srch, 0 - $theta),
                imagerotate_rotateX($srcw, $srch, 0 - $theta)
            );
            $minX = floor(min($temp));
            $maxX = ceil(max($temp));
            $width = $maxX - $minX;

            // Calculate the height of the destination image.
            $temp = array(
                imagerotate_rotateY(0, 0, 0 - $theta),
                imagerotate_rotateY($srcw, 0, 0 - $theta),
                imagerotate_rotateY(0, $srch, 0 - $theta),
                imagerotate_rotateY($srcw, $srch, 0 - $theta)
            );
            $minY = floor(min($temp));
            $maxY = ceil(max($temp));
            $height = $maxY - $minY;
        }

        $destimg = imagecreatetruecolor($width, $height);
        if ($ignore_transparent == 0) {
            imagefill($destimg, 0, 0, imagecolorallocatealpha($destimg, 255,255, 255, 127));
            imagesavealpha($destimg, true);
        }

        // sets all pixels in the new image
        for ($x = $minX; $x < $maxX; $x++) {
            for ($y = $minY; $y < $maxY; $y++) {
                // fetch corresponding pixel from the source image
                $srcX = round(imagerotate_rotateX($x, $y, $theta));
                $srcY = round(imagerotate_rotateY($x, $y, $theta));
                if ($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch) {
                    $color = imagecolorat($srcImg, $srcX, $srcY);
                } else {
                    $color = $bgcolor;
                }
                imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
            }
        }

        return $destimg;
    }

}
帥小哥 2024-11-02 19:16:40

您是否正在运行 Ubuntu?那么可能会解释为什么您会收到错误消息。

Are you running Ubuntu by any chance? Then this might explain why you get the error message.

自在安然 2024-11-02 19:16:40

首先在你的脚本中添加一个 if:

if(!extension_loaded('gd'))
             throw new Exception('GD extenstion not installed');

如果它工作正常,请通过以下方式检查你的 GD 信息:

echo '<pre>';
var_dump(gd_info());

并检查是否支持 jpeg(它应该在 dg_info 结果中)。

如果一切正常但仍然无法正常工作,请询问您的托管服务。

(错误表明您使用了不存在的函数,您可以在使用它的地方粘贴一些代码吗?)

First add to your script an if:

if(!extension_loaded('gd'))
             throw new Exception('GD extenstion not installed');

If its working check out your GD info by:

echo '<pre>';
var_dump(gd_info());

And also check that jpeg is supported (it should be in dg_info results).

If it's all okay and it's still don't works, ask your hosting service.

(Error says that you using non existing function, cna you pase some code where you are using it?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文