利用PHP实现文字绘制到新的图片上的功能

思想和行动 总有一个不能落后于人


虽然写出来了js实现文字和图片加载到一起生成一个新的图片的功能,但是我没有用js实现一个把生成的图片下载到本地的过程,请赌神看了看,赌神说他是用的php写的,我想既然他用php写了,我用js就不太方便获得指导啊,于是乎就顺便看了一下如何用php实现该功能。

百度了一下发现有现成的代码,所以为了方便我也就不浪费时间了,直接copy了人家的代码。



function generateImg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') {
	$date = '' . date ( 'Ymd' ) . '/';
	$img = $date . md5 ( $source . $text1 . $text2 . $text3 ) . '.jpg';
	if (file_exists ( './' . $img )) {
		return $img;
	}

	$main = imagecreatefromjpeg ( $source );

	$width = imagesx ( $main );
	$height = imagesy ( $main );

	$target = imagecreatetruecolor ( $width, $height );

	$white = imagecolorallocate ( $target, 255, 255, 255 );
	imagefill ( $target, 0, 0, $white );

	imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );

	$fontSize = 18;//磅值字体
	$fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB颜色
	$fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质
	imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 190, $fontColor, $font, $text1 );

	$fontBox = imagettfbbox($fontSize, 0, $font, $text2);
	imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 370, $fontColor, $font, $text2 );

	$fontBox = imagettfbbox($fontSize, 0, $font, $text3);
	imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 560, $fontColor, $font, $text3 );

	//imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用

	imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在CV 中,可以使用 `cv::putText()` 函数在图片绘制文字。这个函数的基本用法如下: ```cpp cv::putText(img, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin); ``` 其中: - `img`:要绘制文字图片; - `text`:要绘制文字内容; - `org`:文字的左下角在图片上的坐标; - `fontFace`:字体类型,例如 `cv::FONT_HERSHEY_SIMPLEX`; - `fontScale`:字体大小; - `color`:字体颜色,例如 `cv::Scalar(0, 255, 0)` 表示绿色; - `thickness`:文字线条宽度,默认为 1; - `lineType`:线条类型,默认为 `cv::LINE_8`; - `bottomLeftOrigin`:是否从左下角开始绘制,默认为 false,即从左上角开始绘制。 下面是一个简单的示例代码,演示了如何在图片绘制文字: ```cpp #include <opencv2/opencv.hpp> int main() { cv::Mat img = cv::imread("test.png"); if(img.empty()) { return -1; } std::string text = "Hello, OpenCV!"; cv::Point2f org(50, 50); int fontFace = cv::FONT_HERSHEY_SIMPLEX; double fontScale = 1; cv::Scalar color(0, 255, 0); int thickness = 2; int lineType = cv::LINE_8; bool bottomLeftOrigin = false; cv::putText(img, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin); cv::imshow("Image", img); cv::waitKey(); return 0; } ``` 在这个示例中,我们首先读取了一张图片,然后使用 `cv::putText()` 函数在图片绘制了一段文字。最后,我们使用 `cv::imshow()` 函数显示了绘制结果。 运行上述代码,可以看到绘制了一段文字图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值