Home > Backend Development > PHP Tutorial > PHP image watermark code

PHP image watermark code

不言
Release: 2023-03-25 08:26:02
Original
1738 people have browsed it

This article mainly introduces the code about PHP image watermark, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

function she_zhi_shui_ying($data){
        // 图片地址
        $dst_path = str_replace('public', 'storage', $data->path);
        // 水印文字
        $str = $data->mendian_men_dian_ping_pai.' '.$data->mendian_men_dian_hao.' '.$data->mendian_name."\n".date("Y-m-d H:i", strtotime($data->xun_kai_si_time)).'-'.date("H:i", strtotime($data->xun_jie_shu_time))."\n".$data->created_user_name."\n".$data->canshu_name;

        $fileName = $this->wenn_jian_ming_cheng($dst_path);
        
        $path = 'suiying/'.$fileName.'.jpg';

        if(is_file($path)){
            return $path;
        }else{  
            // 获取图片信息
            $image_info = getimagesize($dst_path);
            // 图片高度
            $imageHeight = $image_info[1];
            // 图片宽度
            $imageWidth = $image_info[0];

            //创建图片的实例
            $dst = imagecreatefromstring(file_get_contents($dst_path));
            //打上文字
            $font = 'fonts/msyhbd.ttf';//字体
            $black = imagecolorallocatealpha($dst, 255, 255, 255, 63);//字体颜色
            imagefttext($dst, 15, 0, 30, $imageHeight-80, $black, $font, $str);
            //输出图片
            list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);

            switch ($dst_type) {
                case 1://GIF
                    header('Content-Type: image/gif');
                    imagegif($dst, $path);
                    break;
                case 2://JPG
                    // header('Content-Type: image/jpeg');
                    // 显示
                    // imagejpeg($dst);
                    // 保存
                    imagejpeg($dst, $path);
                    break;
                case 3://PNG
                    header('Content-Type: image/png');
                    imagepng($dst, $path);
                    break;
                default:
                    break;
            }

            imagedestroy($dst);
            return $path;
        }
    }
Copy after login

Related recommendations:

PHP mailbox sending

PHP data type


The above is the detailed content of PHP image watermark 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