Maison  >  Article  >  développement back-end  >  Créer un filigrane avec PHP

Créer un filigrane avec PHP

不言
不言original
2018-04-14 15:26:331729parcourir

Cet article présente principalement PHP pour créer des filigranes, qui a une certaine valeur de référence. Maintenant, je le partage avec tout le monde. Les amis dans le besoin peuvent s'y référer

1. Ajoutez des filigranes de texte à l'aide de la fonction imagefttext

.
<?php/**
 * 为图片添加文字水印
 * @param  string $dst_path    原图路径
 * @param  string $font_path   字体存放路径
 * @param  string $string_font 欲添加的文字
 */function textwatermark($dst_path,$font_path,$string_font){
    //创建图片的实例
    $dst = imagecreatefromstring(file_get_contents($dst_path));    //添加文字
    $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);
    imagefilledrectangle($dst, 0, 0, 79, 49, 0x0000FF);
    imagefilledrectangle($dst, 9, 9, 70, 40, 0xFFFFFF);
    imagefttext($dst, 13, 0, 20, 20, $black, $font_path, $string_font);    //输出图片
    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);    switch ($dst_type) {        case 1://IMAGETYPE_GIF
            header(&#39;Content-Type: image/gif&#39;);
            imagegif($dst);            break;        case 2://IMAGETYPE_JPEG
            header(&#39;Content-Type: image/jpeg&#39;);
            imagejpeg($dst);            break;        case 3://IMAGETYPE_PNG
            header(&#39;Content-Type: image/png&#39;);
            imagepng($dst);            break;        default:            break;
    }
    imagedestroy($dst);
}
header(&#39;charset=utf-8&#39;);$dst_path = &#39;./uploads/1.jpg&#39;;//选择的字体需支持中文 arial.ttf不支持中文$font_path = &#39;C:/Windows/Fonts/simhei.ttf&#39;;        
//当文件编码为utf-8时 不需转换 $string_font = &#39;剑liang&#39;;       
textwatermark($dst_path,$font_path,$string_font);?>

2. Utilisez la fonction imagecopymerge pour les filigranes d'images

<?php/**
 * 添加图片水印功能
 * @param resource $dst_path 原图路径
 * @param resource $src_path 水印图片路径
 * @param int $pact 水印合并效果,默认为50 
 * @param int $postion 添加水印位置,默认为右下角
 */function watermark($dst_path,$src_path, $pct = 50,$postion = 5){
    //创建图片的实例
    $dst = imagecreatefromstring(file_get_contents($dst_path));    $src = imagecreatefromstring(file_get_contents($src_path));    // 从数组中获取原图和水印图片的宽和高
    list($dst_w, $dst_h) = getimagesize($dst_path);    list($src_w, $src_h) = getimagesize($src_path);    switch ($postion) {       case 1: // 左上
            $src_x = $src_y = 0;                
            break;        case 2: // 右上
            $src_x = $dst_w - $src_w;            $src_y = 0;                        
            break;       case 3: // 中间
           $src_x = ($dst_w - $src_w) / 2;           $src_y = ($dst_h - $src_h) / 2;    
           break;       case 4: // 左下
            $src_x = 0;            $src_y = $dst_h - $src_h;       
            break;       case 5: // 右下
            $src_x = $dst_w - $src_w;            $src_y = $dst_h - $src_h;        
            break;       default:            break;
    }    //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
    imagecopymerge($dst, $src, $src_x, $src_y, 0, 0, $src_w, $src_h, $pct);    //如果水印图片本身带透明色,则使用imagecopy方法
    // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);

    //输出图片
    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);    switch ($dst_type) {        case 1://IMAGETYPE_GIF
            header(&#39;Content-Type: image/gif&#39;);
            imagegif($dst);            break;        case 2://IMAGETYPE_JPEG
            header(&#39;Content-Type: image/jpeg&#39;);
            imagejpeg($dst);            break;        case 3://IMAGETYPE_PNG
            header(&#39;Content-Type: image/png&#39;);
            imagepng($dst);            break;        default:            break;
    }
    imagedestroy($dst);
    imagedestroy($src);
}$source = &#39;./uploads/1.jpg&#39;;$water = &#39;./uploads/6.jpg&#39;;
watermark($source, $water, 50, 5);?>

Recommandations associées :

Étapes détaillées pour créer une session en php

Comment créer ou exporter des tableaux de données Excel avec PHP

Comment créer des images PNG transparentes avec PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn