Home > Backend Development > PHP Tutorial > How to implement image synthesis and generate watermarks in PHP (complete code)

How to implement image synthesis and generate watermarks in PHP (complete code)

不言
Release: 2023-04-04 06:12:01
Original
3520 people have browsed it

The content of this article is about how to implement image synthesis and generate watermarks (complete code) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php
$dst_path =&#39;./upload/goods/2018/06-04/2bd2518a2e4e6d8bdab9f01434b9f754.png&#39;;
$src_path = &#39;./public/upload/weixin/20180620/a643cf1ee7dd5bda12270647ca543f60.png&#39;;
$hz = substr(strrchr($dst_path, &#39;.&#39;), 1);
$image = date("YmdHis").rand(100000,999999).".".$hz;
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取水印图片的宽高
$src_w =139;$src_h=58;
list($src_w,$src_h) = getimagesize($src_path);
//如果水印图片本身带透明色,则使用imagecopy方法
imagecopy($dst, $src, 85,85, 0, 0, $src_w, $src_h);
//输出图片
list($src_w, $src_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
    case 1://GIF

        header(&#39;Content-Type: image/gif&#39;);

        imagegif($dst,".".$image);        
        break;    
    case 2://JPG

        header(&#39;Content-Type: image/jpeg&#39;); 

        imagejpeg($dst,".".$image); 

        break; 

    case 3://PNG 

        header(&#39;Content-Type: image/png&#39;); 

        imagepng($dst,".".$image); 

        break; 

    default: 

        break;
} 

imagedestroy($dst); 

imagedestroy($src); 

return $image;
Copy after login

Related recommendations:

Php upload, generate thumbnails, generate text watermarks and image watermarks

PHP generates images Watermark and text watermark

The above is the detailed content of How to implement image synthesis and generate watermarks in PHP (complete code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template