php - 使用 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);
};
?>
这是有效的,结果如下所示:
问题
有没有办法以一定的角度旋转文本?
解决方案
您可以使用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);
...
调整第二个参数的角度
推荐阅读
- sql - 使用 SQL 查询提取所需数据的安然数据集
- reactjs - Firebase:如何手动将 Google 登录身份验证器限制为特定的电子邮件地址?
- java - Springboot SOAP 消息
- neo4j - Cypher - 获取节点的所有关联关系
- python - BeautifulSoup 不能在 lambda 中工作,但可以在本地 jupyter 中工作
- logstash - Logstash:有没有办法在一条消息中解析多行?
- git - 为什么我的 .git/ref 文件夹中没有列出 .git/FETCH_HEAD 文件中的所有引用?
- tensorflow - 用于图像分类的自定义 Max-Pooling 层
- r - 与 ggplotly() 结合使用时,ggplot2 堆叠条似乎被分成许多不同的切片
- string - 在这种情况下可以使用 Lambda 函数,还是只是一个 def 函数?