php - 调用未定义函数 imagerotate()

标签 php image-processing gd

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

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

第 30 行:

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

根据 the PHP documentation , 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 支持启用

最佳答案

page你链接到它清楚地说

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

还有一个comments

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.

有关可能的解决方案,请查看 here

代码复制自 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;
    }

}

关于php - 调用未定义函数 imagerotate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5406771/

相关文章:

PHP代码没有被执行,但代码显示在浏览器源代码中

php - 在每个函数调用 php 上执行一些东西

javascript - 如何删除数组中的重复项? (不是变成唯一的,而是删除下一个值) PHP

angular - Ionic 3 图像延迟加载器

excel - 如果该区域已被填充,则防止修补

c++ - 使用tesseract 3.01的页面布局分析出现的奇怪问题

PHP图像调整大小/缩略图方法(Wordpress)

php - 未初始化的字符串偏移量: 62

php - 良好的图像调整框架

javascript - FormData 控制台记录 [object FormData ] 而不是实际文件