php - 使用 imagestring() 旋转文本

标签 php

我目前正在做一个小项目。对于这个项目,我需要一张带有文字的照片(让机器人更难找出文字)。
我正在听从 this 的建议回答,但是 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);    
    };
?>
这是有效的,结果如下所示:
example
问题
有没有办法以一定的角度旋转文本?

最佳答案

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

    ...
    //Adds white-colored text
    $var = imagestring($img, 5, 18, 18, $random . " = ?", $white);
    $rotate = imagerotate($img, 10, 0);
    //Save image
    imagejpeg($rotate, "exercise.png", -1);   
    ...
调整角度的第二个参数

关于php - 使用 imagestring() 旋转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63700672/

相关文章:

php - 使用 Paypal 沙盒逐步创建结帐表单 (PHP)

PHP:使用函数内部的变量

php - 从 Woocommerce 中的循环中排除产品类别

php - 两个比较在一个 if/else 语句中

php - 如何在不使用主键的情况下更新 Yii 中的行?

php - php中如何在不刷新页面的情况下获取cookie?

php - 将内容部署到多个服务器 (EC2)

javascript - Ajax 在这种情况下不起作用,我不知道为什么

php - 编译 PHP 以防止 "theft"?

php - 使用Youtube API检索视频标题和关联ID