php 图片水印中文乱码解决方法

 更新时间:2016年11月25日 16:58  点击:2336
php 图片水印中文乱码解决方法是要转入一款中文字体摩洛哥 以了,如果你在生成水印有中文时,又没载入相关的字体的话,那中文肯定会是乱码的,如果是英语字母那载不载入都没关系哦。
 代码如下 复制代码
$name =  iconv("gb2312","utf-8",www.111cn.net 一聚教程网);
$font = 'simhei.ttf';//水印字体  
$im = imagecreatefromjpeg("test.jpg");//载入图片  
$black = imagecolorallocate($im, 0, 0, 0);//设置颜色  
imagettftext($im, 12, 0, 320, 84, $black, $font, $name);//打印水印  
imagepng($im);//输出图片,如果要保存加个保存路径和文件名,如imagepng($im,'test1.jpg');  
imagedestroy($im);//清空缓存 


下面来看一款生成水印文字函数

 代码如下 复制代码

function str2pic ($string,$source,$destination="",$f,$fontsize=10,$shadowcolor="#ffffff",$f,$x=10,$y=10) {  
 //header('content-type:image/png');
    $pi=pathinfo($source);
    $pie=$pi[extension];#获取扩展名
    if(eregi("jpg|jpeg",$pie))$im=@imagecreatefromjpeg($source);
    if(eregi("gif",$pie))$im=@imagecreatefromgif($source);
    if(eregi("png",$pie))$im=@imagecreatefrompng($source);
    $col1=hex2dec($shadowcolor);#阴影颜色
    $col2=hex2dec($fontcolor);#字体颜色
    $col1=imagecolorallocate($im,$col1[0],$col1[1],$col1[2]);
    $col2=imagecolorallocate($im,$col2[0],$col2[1],$col2[2]);
    imagettftext($im,$fontsize,0,$y+1,$x+1,$col1,$fonturl,$string);
    imagettftext($im,$fontsize,0,$y,$x,$col2,$fonturl,$string);
    imagejpeg($im);
    if($destination)imagejpeg($im,$destination);
    imagedestroy($im);
}

function hex2dec($hexcolror) {#十六进制颜色转换成10进制颜色
    preg_match_all("/([0-f]){2,2}/i",$hexcolror,$matches);
    if(count($matches[0])==3){
    $rr=hexdec($matches[0][0]);
    $gg=hexdec($matches[0][1]);
    $bb=hexdec($matches[0][2]);
    }
    return array($rr,$gg,$bb);
}
?>
<?php
str2pic("[url=http://www.111cn.net]www.111cn.net","winter.jpg","winter2.jpg","simhei.ttf",10,"ffffff","330099",10,10[/url]);
?>

 

 代码如下 复制代码

//adv0.jpg就是背景图片,注意函数与图片格式对应  
$im = imagecreatefromjpeg('/www/law/images/demo/adv0.jpg');   
$font_color = imagecolorallocate ($im, 0, 250, 10); //这是文字颜色,绿色  
 
$text = "张三的博客";                               //文字内容  
 
$font_file = "/www/font/hyi_xkj.ttf";               //字体的linux绝对路径  
 
//26:字体, 0 是角度, 10,36是坐标, $font_color是文字色, font是字体,  文本是填入的文字  
imagettftext($im, 26,0, 10, 36, $font_color ,$font_file, $text);  往图片插入文字  
 
// output image  
header ('content-type: image/png');                 //即便是从jpg拷贝的图片,也能以png输出,  
imagepng ($im);  
// clean up  
imagedestroy($im);


生成水印方法二

 代码如下 复制代码

public final class imageutils {
public imageutils() {

}

public final static string getpressimgpath(){
return applicationcontext.getrealpath("/template/data/util/shuiyin.gif");
}

/**
* 把图片印刷到图片上
* @param pressimg -- 水印文件
* @param targetimg -- 目标文件
* @param x
* @param y
*/
public final static void pressimage(string pressimg, string targetimg, int x, int y) {
try {
file _file = new file(targetimg);
image src = imageio.read(_file);
int wideth = src.getwidth(null);
int height = src.getheight(null);
bufferedimage image = new bufferedimage(wideth, height,
bufferedimage.type_int_rgb);
graphics g = image.creategraphics();
g.drawimage(src, 0, 0, wideth, height, null);

// 水印文件
file _filebiao = new file(pressimg);
image src_biao = imageio.read(_filebiao);
int wideth_biao = src_biao.getwidth(null);
int height_biao = src_biao.getheight(null);
g.drawimage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
height_biao, null);
// /
g.dispose();
fileoutputstream out = new fileoutputstream(targetimg);
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
encoder.encode(image);
out.close();
} catch (exception e) {
e.printstacktrace();
}
}

/**
* 打印文字水印图片
* @param presstext --文字
* @param targetimg -- 目标图片
* @param fontname -- 字体名
* @param fontstyle -- 字体样式
* @param color -- 字体颜色
* @param fontsize -- 字体大小
* @param x -- 偏移量
* @param y
*/

public static void presstext(string presstext, string targetimg, string fontname,int fontstyle, int color, int fontsize, int x, int y) {
try {
file _file = new file(targetimg);
image src = imageio.read(_file);
int wideth = src.getwidth(null);
int height = src.getheight(null);
bufferedimage image = new bufferedimage(wideth, height,
bufferedimage.type_int_rgb);
graphics g = image.creategraphics();
g.drawimage(src, 0, 0, wideth, height, null);
// string s=www.111cn.net;
g.setcolor(color.red);
g.setfont(new font(fontname, fontstyle, fontsize));


g.drawstring(presstext, wideth - fontsize - x, height - fontsize/2 - y);
g.dispose();
fileoutputstream out = new fileoutputstream(targetimg);
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
encoder.encode(image);
out.close();
} catch (exception e) {
system.out.println(e);
}
}

public static void main(string[] args) {
pressimage("c:/shuiyin/shuiyin.gif", "c:/shuiyin/dsc02342.jpg", 20 ,20);
}
}

在用户验证页面,如注册,登录的时候,为了加强用户登录的安全性,添加验证码验证代码,下面我为各位朋友提供一四款不同同类型的php验证代码程序,最后一款是一款使用了验证代码的实例代码。
 代码如下 复制代码
date_default_timezone_set('asia/shanghai');
 function setcode($len)
 {
  $code = '';
  for ($i=0;$i<$len;$i++)//生成随机长度
  {
   $code .= chr(drand());
  }
  return $code;
 }
 //生成一个随机字符
 function drand()
 {
  $rand = mt_rand(0,2);
  $str = '';
  switch ($rand)
  {
   case 0: $str = mt_rand(48,57);break;//数字
   case 1: $str = mt_rand(65,90);break;//大写字母
   case 2: $str = mt_rand(97,122);break;//小写字母
  }
  return $str;
 }
 $_session['checkcode'] = $code = setcode(5);

//php图片验证码原代码,需支持gd2.dll扩展,需修改php.ini后重启iis or ap

 代码如下 复制代码

session_start();
session_register('safecode');
$type = 'gif';
$width= 40;
$height= 16;
header("content-type: image/".$type);
srand((double)microtime()*1000000);
$randval = randstr(4,"");
if($type!='gif' && function_exists('imagecreatetruecolor')){
     $im = @imagecreatetruecolor($width,$height);
}else{
     $im = @imagecreate($width,$height);
}
     $r = array(225,211,255,223);
     $g = array(225,236,237,215);
     $b = array(225,236,166,125);

     $key = rand(0,3);
 
     $backcolor = imagecolorallocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
     $bordercolor = imagecolorallocate($im, 0, 0, 0);//边框色
     $pointcolor = imagecolorallocate($im, 255, 170, 255);//点颜色

     @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backcolor);//背景位置
     @imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor); //边框位置
     $stringcolor = imagecolorallocate($im, 255,51,153);

     for($i=0;$i<=100;$i++){
           $pointx = rand(2,$width-2);
           $pointy = rand(2,$height-2);
           @imagesetpixel($im, $pointx, $pointy, $pointcolor);
     }

     @imagestring($im, 3, 5, 1, $randval, $stringcolor);
     $imagefun='image'.$type;
     $imagefun($im);
     @imagedestroy($im);
     $_session['safecode'] = $randval;
//产生随机字符串
function randstr($len=6,$format='all') {
           switch($format) {
                 case 'all':
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789www.111cn.net'; break;
                 case 'char':
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; break;
                 case 'number':
                 $chars='0123456789'; break;
                 default :
                 $chars='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';
                 break;
           }
     $string="";
     while(strlen($string)<$len)
     $string.=substr($chars,(mt_rand()%strlen($chars)),1);
     return $string;
}

 

//调用此页面,如果下面的式子成立,则生成验证码图片
if($_get["action"]=="verifycode")
{
    rand_create();
}
//验证码图片生成
function rand_create()
{
    //通知浏览器将要输出png图片
    header("content-type: image/png");
    //准备好随机数发生器种子 
    srand((double)microtime()*1000000);
    //准备图片的相关参数  
    $im = imagecreate(62,20);
    $black = imagecolorallocate($im, 0,0,0);  //rgb黑色标识符
    $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
    $gray = imagecolorallocate($im, 200,200,200); //rgb灰色标识符
    //开始作图    
    imagefill($im,0,0,$gray);
    while(($randval=rand()%100000)<10000);{
        $_session["login_check_num"] = $randval;
        //将四位整数验证码绘入图片 
        imagestring($im, 5, 10, 3, $randval, $black);
    }
    //加入干扰象素   
    for($i=0;$i<200;$i++){
        $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
    }
    //输出验证图片
    imagepng($im);
    //销毁图像标识符
    imagedestroy($im);
}
//检验验证码
function rand_check()
{
    if($_post["reg_rand"] == $_session["login_check_num"]){
        return true;
    }
    else{
        exit("验证码输入错误");
    }
}

//验证码通过gd生成png图片,并把$randval随机数字赋给$_session['login_check_num'],在通过用户输入的$_post进行比较,来判断是否正确。达到需要实现的功能,需要修改php.ini文件,使php支持gd库

本程序是根据用户提供的数据生成3D效果的百分比图片统计效果有一点像中国站长

$image = imagecreatetruecolor(200,200);  //创建一张200*200的画布;

 代码如下 复制代码

//创建多种又区分的颜色
$red = imagecolorallocate($image,255,0,0);
$blue  = imagecolorallocate($image,0,0,255);
$yellow = imagecolorallocate($image,255,255,0);
$violet = imagecolorallocate($image,255,0,255);
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,0,0,0);


//使用for循环创建3d效果底层效果
for($i=120;$i>100;$i--){
    imagefilledarc($image,100,$i,200,120,0,30,$red,img_arc_pie);//img_arc_pie注释如下:

    imagefilledarc($image,100,$i,200,120,30,80,$blue,img_arc_pie);
    imagefilledarc($image,100,$i,200,120,80,360,$yellow,img_arc_pie);
}
//bool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
//
//imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。如果成功则返回 true,失败则返回 false。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(or)后的值:
//
//img_arc_pie
//
//img_arc_chord
//
//img_arc_nofill
//
//img_arc_edged


//这个层是最上面一层的效果,这样立体效果就出来了!
    imagearc($image,100,100,200,120,0,360,$black);//添加一个黑色的边圈,这样3d效果看起来更加明显点
    imagefilledarc($image,100,100,200,120,0,30,$red,img_arc_pie);
    imagefilledarc($image,100,100,200,120,30,80,$blue,img_arc_pie);
    imagefilledarc($image,100,100,200,120,80,360,$yellow,img_arc_pie);

//添加百分比数据,当然此处必要的时候可以批量的进行一定的运算将输入输入到图片上
    $str = iconv ("gbk","utf-8","36%");//如果要输入中文需要此转换。example:占用:30%;
    imagettftext($image,10,360-15,100+70,115,$white,"simhei.ttf",$str);

 

imagejpeg($image);
imagedestroy($image);

 

这是一款经典实用的生成小图的php代码,有专业素语来讲就是php 生成缩略图代码哦。

# 显示图形及连接

 function showdir ($adirectory, $i)

{
  global $browsedir;

  $start = $i;

# 更改 $maxcols 及 $maximages 可改变每一页显示的小图的行数与列数。

  $maxcols = 2;
  $maximages = 6;
  $maximages = $i + ($maximages - 3);

# 更改 $imagemaxwidth 及 $imagemaxheight 可改变显示小图的宽度与高度。

  $imagemaxwidth = 100;
  $imagemaxheight = 100;
   
# 计算高度与宽度的比例。

  $imagemaxratio =  $imagemaxwidth / $imagemaxheight;
   
  $ndirectory = sizeof ($adirectory);
  echo (table_start);
  for ($i; $i<=$maximages;)
  {
     echo (row_start);
     for ($icols=1; $icols<=$maxcols; $icols++)
     {
       echo (col_start);
       $imagefilename = $adirectory[++$i];
       if (strlen($imagefilename)>0)
       {
         $imagepath = $browsedir."/".$imagefilename;
         $imagesize = getimagesize ($imagepath);
         if ($imagesize)
         {
           $imagewidth = $imagesize[0];
           $imageheight = $imagesize[1];
           $imageratio = $imagewidth / $imageheight;
           if ($imageratio > $imagemaxratio)
           {
              $imageoutputwidth = $imagemaxwidth;
              $imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
           }
           else if ($imageratio < $imagemaxratio)
           {
              $imageoutputheight = $imagemaxheight;
              $imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
           } else
           {
              $imageoutputwidth = $imagemaxwidth;
              $imageoutputheight = $imagemaxheight;
           }

# 显示图形

           echo (a_start.$imagepath.a_close);
           echo (img_start.$imagepath.img_width.$imageoutputwidth.img_height.$imageoutputheight.img_end);
           echo (line_break.$adirectory[$i]);
           echo (a_end);
         }
         echo (col_end);
       }
     }
     echo (row_end);
  }
  echo (table_end);
pagemenu ($browsedir, $ndirectory, $start);

}

function pagemenu ($browsedir, $ndirectory, $pg) {

echo "<br><center><font face="verdana, arial, helvetica, sans-serif" size="1" color="#000033">page:";

$pg_num = 1;

for ($img_num = 0; $img_num <= $ndirectory;) {

    if ($pg == $img_num) {
    echo "<span class="menulink_1"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> *$pg_num</a> <span>";
    } else {
    echo "<span class="menulink_2"><a href="thumb.php?browsedir=$browsedir&start=$img_num"> $pg_num</a> <span>";
    }

# 建立其他页次的连接, 每页显示四张图, 故页数 $pg_num 每加 1 , $img_num 就加 4 。

    $pg_num = $pg_num + 1;
    $img_num = $img_num + 4;

}

echo "</font> ";

}

function dirtoarray ($browsedir, $extensions)
{

  $nextensions = sizeof ($extensions);
  $idirectory = 0;
  $directory = dir($browsedir);
   
  while ($entry = $directory->read())
  {
      for ($i=1; $i<=$nextensions; $i++)
      {
         $compare = stristr ($entry, $extensions[$i]);
         if (strlen($compare) == strlen($extensions[$i]))
         {
            $adirectory[++$idirectory] = $entry;
            break;
         }
      }
  }
  $directory->close();
  return $adirectory;
}

#主程序

#变量 $browsedir 为图形文件放置的位置。

$browsedir="./images";

# 允许浏览的图形文件扩展名, 放置於数组中, 可自行增加。

$extensions[1] = "jpeg";
$extensions[2] = "jpg";
$extensions[3] = "gif";
$extensions[4] = "png"; 
showdir (dirtoarray ($browsedir, $extensions), $start);

define ("line_break", "<br>");
define ("table_start", "<table width=600> ");
define ("table_end", "</table> ");
define ("row_start", "  <tr> ");
define ("row_end", "  </tr> ");
define ("col_start", "   <td align=center>       ");
define ("col_end", "    </td> ");
define ("img_start", "<img src=");
define ("img_end", ">");
define ("img_width", " width=");
define ("img_height", " height=");
define ("a_start", '<a href="');
define ("a_close", '">');
define ("a_end", "</a>");
?>

[!--infotagslink--]

相关文章

  • php生成二维码中文乱码问题解决方法

    最近做了个扫描二维码得到vcard的项目,遇到一个问题,有一部分生成完的二维码,用android系统手机扫描后得到的vcard中的中文姓名是乱码,经过比对发现,这部分vcard中ORG这个...2016-11-25
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • HTTP 408错误是什么 HTTP 408错误解决方法

    相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
  • 运行vbs脚本报错无效字符、中文乱码的解决方法(编码问题)

    今天在写一个vbs的时候,发现中文乱码,后来写好代码正常运行的代码压缩一下给了同事,发现报无效字符,经过验证后发现原来是编码的问题导致,这里就为大家分享一下...2020-06-30
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • 安卓手机wifi打不开修复教程,安卓手机wifi打不开解决方法

    手机wifi打不开?让小编来告诉你如何解决。还不知道的朋友快来看看。 手机wifi是现在生活中最常用的手机功能,但是遇到手机wifi打不开的情况该怎么办呢?如果手机wifi...2016-12-21
  • 连接MySql速度慢的解决方法(skip-name-resolve)

    最近在Linux服务器上安装MySql5后,本地使用客户端连MySql速度超慢,本地程序连接也超慢。 解决方法:在配置文件my.cnf的[mysqld]下加入skip-name-resolve。原因是默认安装的MySql开启了DNS的反向解析。如果禁用的话就不能...2015-10-21
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • 总结android studio注意事项及打不开等问题解决方法

    经过一段时间的使用,总结了android studio打不开等问题的6种解决方法及android studio注意事项,希望对大家有所帮助。 1 首次运行,建立好项目需要下载一些东西,如果...2016-09-20
  • PHP json_encode() 函数详解及中文乱码问题

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。这个函数的功能是将数值转换成json数据存储格式。<&#63;php$arr = array ( 'Name'=>'希亚', 'Age'...2015-11-08
  • MySQL ERROR 2013 (HY000)错误解决方法

    当通过 TCP/IP 连接 MySQL 远程主机时,出现 ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 104 。如果是在linux shell命令行中直接打 mysql 命令,...2015-03-15
  • IE6-IE9中tbody的innerHTML不能赋值的解决方法

    IE6-IE9中tbody的innerHTML不能赋值,重现代码如下 复制代码 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>IE6-IE9中tbody的innerHTML不能复制bug</title> </head> <body style="height:3...2014-06-07
  • Mysql修改datadir导致无法启动问题解决方法

    centos6.2,停止mysqld然后修改/etc/my.cnf datadir的位置,启动mysqld提示FAILED,查看日志 复制代码 代码如下: 120609 11:31:31 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended 120609 11:35:12 my...2015-03-15
  • 小米解锁验证失败怎么办 小米解锁工具登录失败解决方法

    小米手机如果想要刷机就必须要先解锁验证才可以,那么,如果遇到小米解锁验证失败以及小米解锁工具登录失败的现象怎么办呢?对此,本文就为大家进行解答,有需要的朋友来看看。...2016-12-21
  • Photoshop提示“此产品的许可证已过期"及“无法开始您的Adobe Photoshop CS5.1 订阅”解决方法

    首先不要鄙视我用Photoshop盗版,实在是贵。现在我们来看看如果你的Photoshop提示“此产品的许可证已过期",还有“无法开始您的Adobe Photoshop CS5.1 订阅”如何解决吧...2016-09-14
  • 解决HttpPost+json请求---服务器中文乱码及其他问题

    这篇文章主要介绍了解决HttpPost+json请求---服务器中文乱码及其他问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-22
  • photoshop字体显示乱码解决方法

    今天小编在这里就来给各位photoshop的这一款软件的使用者们来详细的说一下软件的字体出现了乱码这一问题的解决方法,那么各位有出现这个问题的,下面就来跟着小编一起看...2016-09-14
  • php中iconv编码转换来解决中文乱码的问题

    用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些 代码如下 复制代码 ...2016-11-25
  • 51安卓模拟器启动失败解决方法

    有部分小伙伴表示自己遇到了51模拟器程序不能打开的情况,那么51模拟器打不开怎么回事?下面我就来为大家分享一下解决的方法,有需要的小伙伴就来看一看吧。 &#8195;&...2017-07-06
  • CentOS下php使用127.0.0.1不能连接mysql的解决方法

    php代码很简单:复制代码 代码如下: $server="127.0.0.1"; println("Begin"); $link = mysql_connect($server,"mysql","mysql"); if (!$link) { die('Could not connect: ' . mysql_error().mysql_errno()); } lin...2015-03-15