首页 > 解决方案 > 使用 imagestring() 旋转文本

问题描述

我目前正在做一个小项目。对于这个项目,我需要一张带有文字的照片(让机器人更难识别文字)。

我正在遵循这个答案的建议,但imagettftext使用的是真正的字体,这在我的情况下是不可能的。因此,我搜索了替代方法并找到了imagestring.

所以我解决这个问题的方法是这样的:

<?php
    function randExer() {
        //Creating random (simple) math problem
        $arr = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
        $item1 = $arr[array_rand($arr)];
        $item2 = $arr[array_rand($arr)];
        $random = $item1 . " + " . $item2;

        //Saving created math problem for later
        file_put_contents("exercise.txt", $random);

        //Creates a black picture with width=200 and height = 50
        $img = imagecreatetruecolor(200, 50);

        //uses RGB-values to create a useable color
        $white = imagecolorallocate($img, 255, 255, 255);

        //Adds white-colored text
        $var = imagestring($img, 5, 18, 18, $random . " = ?", $white);

        //Save image
        imagejpeg($img, "exercise.png", -1);    
    };
?>

这是有效的,结果如下所示:

例子

问题

有没有办法以一定的角度旋转文本?

标签: php

解决方案


您可以使用https://www.php.net/manual/en/function.imagerotate.php imagerotate 例如

    ...
    //Adds white-colored text
    $var = imagestring($img, 5, 18, 18, $random . " = ?", $white);
    $rotate = imagerotate($img, 10, 0);
    //Save image
    imagejpeg($rotate, "exercise.png", -1);   
    ...

调整第二个参数的角度


推荐阅读