php|php 图片水印imagecopymerge和imagecopy

PHP给图片加水印常用imagecopymerge和 imagecopy两种合成方法。
imagecopymerge可以把不符合大小尺寸的图片压缩或拉伸成合适的水印大小,并对整个水印图片加透明度,但水印图片内部的透明度会被填充为默认黑色,例如带圆角的图片。
【php|php 图片水印imagecopymerge和imagecopy】imagecopy可以对原图素材裁剪,但不做压缩或填充。合成后保留png本身的透明度,例如圆角logo。

//作为打底的图片宽度600高度700 $bgsrc = https://www.it610.com/article/BASE_PATH.'/public/files/qrcode_bg.png'; $info = getimagesize($bgsrc); $type = image_type_to_extension($info[2], false); $createImageFunc = "imagecreatefrom{$type}"; $image = $createImageFunc($bgsrc); //二维码 $imageMark = BASE_PATH.'/public/files/qrcode.png'; $markInfo = getimagesize($imageMark); $markType = image_type_to_extension($markInfo[2], false); $markCreateImageFunc = "imagecreatefrom{$markType}"; $waterSrc = https://www.it610.com/article/$markCreateImageFunc($imageMark); $mask_width=342; $mask_height=342; $water = imagecreatetruecolor($mask_width, $mask_height); //进行压缩或拉伸有填充色 imagecopyresampled($water, $waterSrc, 0, 0, 0, 0, $mask_width, $mask_height, $width, $height); imagecopymerge($image, $water, 130, 183, 0, 0, $mask_width, $mask_height, 100); imagedestroy($water); //带圆角的logo $imageMark = BASE_PATH.'/public/files/logo.png'; list($width, $height) = getimagesize($imageMark); $markInfo = getimagesize($imageMark); $markType = image_type_to_extension($markInfo[2], false); $markCreateImageFunc = "imagecreatefrom{$markType}"; $waterSrc = https://www.it610.com/article/$markCreateImageFunc($imageMark); //无任何压缩拉伸只做裁剪可透明 imagecopy($image,$waterSrc,264,324,0,0,$width, $height); imagedestroy($waterSrc); //添加字体水平居中 $font = BASE_PATH.'/public/files/font/msyhbd.ttf'; $fontSize=28; $fontColor = imagecolorallocate($image, 255, 255, 255); //设置颜色 rgb $fontBox = imagettfbbox($fontSize, 0, $font, $team->title); //文字水平居中实质 imagettftext($image, $fontSize, 0, ceil((600 - $fontBox[2]) / 2), 78, $fontColor, $font, $team->title); //打印//合成水印 $outputfunc = "image{$type}"; $fullCoverPath = BASE_PATH.'/public/files/qrcode_card.png'; $outputfunc($image, $fullCoverPath); //保存路径 imagedestroy($image);

    推荐阅读