最近在复习用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);


THE END

本站部分文章搜集整理于互联网或者网友提供,如有侵权请联系站长

如若转载,请注明出处:https://www.htmlbk.com/php/987.html

温馨提示:该文档最后一次修改时间为2022-07-19 12:04:08,请注意相关的内容是否还可用!