PHP根据PNG图像生成新图并添加文字

该图像为PNG带有透明背景。图像实质是给某站点产品图上部添加打折信息的图标。由于ie系列浏览器无css旋转,遂用php将打折后文字写至图片上。

原图:

处理详细代码:

header("Content-type: image/png");
$discount= 60;//此处省略获取该数值途径
$font_path = 'arial.ttf';//字体文件的路径
$oldimage_name = 'off.png';//原始图片路径
$image_src = imagecreatefrompng($oldimage_name); //根据原始图片创建新图
imagesavealpha($image_src,true);//至关重要:保留原图alpha通道,否则png透明部分会被添黑
$color = imagecolorallocate($image_src, 255, 255, 255);//字体颜色
imagettftext($image_src, 16, -45, 15, 10, $color, $font_path, $discount);//将文字以16号字-45度角绘制到15,10坐标点
imagepng($image_src);
原文地址:https://www.cnblogs.com/liuxgnu/p/3535109.html