php图片合成【png图片】
php 图片合成【png图片】
- 示例代码
<?php
header("Content-type:text/html;charset=utf-8");
error_reporting(E_ALL);
define("WWWROOT",str_replace(DIRECTORY_SEPARATOR,'/',str_ireplace(str_replace("/","\\",$_SERVER['PHP_SELF']),'',__FILE__)."\\"));
$img1 = WWWROOT."resource/big.png";
$img2 = WWWROOT."resource/small.png";
$image_1 = imagecreatefrompng($img1);
$image_2 = imagecreatefrompng($img2);
$left = intval((imagesx($image_1) - imagesx($image_2)) / 2);
$top = intval((imagesy($image_1) - imagesy($image_2)) / 2);
imagecopymerge($image_1, $image_2, $left, $top, 0, 0, imagesx($image_2), imagesy($image_2), 100);
$merge = WWWROOT."resource/merge1.png";
$down = imagepng($image_1, $merge);
if ($down) {
return $merge;
} else {
return false;
}
- 代码参考补充
//参数 活动模板图片,二维码url,模板内二维码的位置
function getActivityImg($template,$url,$x,$y)
{
require_once library_path("/phpqrcode.php");
//二维码中间添加logo
$logo = public_path('/assets/img/logos/logo.png');
$QR = "base.png";
$last = "last.png";
$errorCorrectionLevel = 'Q'; //防错等级
$matrixPointSize = 8; //二维码大小
//生成二维码
//参数内容:二维码储存内容,生成存储,防错等级,二维码大小,白边大小
QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);
//合并logo跟二维码-----------------start
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width / 5;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
imagepng($QR,$last); // 生成带log的二维码图片 存储到last
//合并logo跟二维码-----------------end
//合成带logo的二维码图片跟 模板图片--------------start
$path_1 = $template;
$path_2 = $last;
$image_1 = imagecreatefromjpeg($path_1);
$image_2 = imagecreatefrompng($path_2);
$image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
$color = imagecolorallocate($image_3, 255, 255, 255);
imagefill($image_3, 0, 0, $color);
imageColorTransparent($image_3, $color);
imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
imagecopymerge($image_3, $image_2, $x, $y,0, 0, imagesx($image_2), imagesy($image_2), 100);
//合成带logo的二维码图片跟 模板图片--------------end
//输出到本地文件夹
$fileName=md5(basename($template).$url);
$EchoPath='/assets/img/'.$fileName.'.png';
imagepng($image_3,public_path($EchoPath));
imagedestroy($image_3);
//返回生成的路径
return $EchoPath;
}
正因为来之不易,所以才有了后来的倍加珍惜。