2024年03月25日 php生成图片 php合成图片 php修改图片底色 php生成海报 php文字生成图片 懒猪技术
最近在复习用PHP直接生成图片,如本博客“朝花夕拾”栏目文档缩略图就是直接PHP合成的,这里贴出部分代码,以便方便自己后续有用到的时候可以直接拿来用。
话不多说,代码如下:
<?php
$filePath = ''; //png的完整路径,包括文件名和扩展名
$savePath = ""; //保存的png的完整路径,包括文件名和扩展名
$colorRgb = array('red' => rand(50,200), 'green' => rand(50,200), 'blue' => rand(50,200)); //背景色
$img = imagecreatefrompng($filePath);
$width = imagesx($img);
$height = imagesy($img);
//创建新图像并用背景色填充
$backgroundImg = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($backgroundImg, $colorRgb['red'], $colorRgb['green'], $colorRgb['blue']);
imagefill($backgroundImg, 0, 0, $color);
//将原始图像复制到背景
imagecopy($backgroundImg, $img, 0, 0, 0, 0, $width, $height);
//另存为图片文件
// imagejpeg($backgroundImg, $savePath, 90);
//直接输出
header('Content-Type: image/jpeg');
imagejpeg($backgroundImg);本文链接:http://so.lmcjl.com/news/365/