php图片增加中文与图片水印代码
class smallpic{
private $src_pic;//原图
private $ico_pic = "003.png";//水印图
private $ico_text = "水印";//水印文字
private $small_width;//缩略图宽度
private $small_height;//缩略图高度
private $is_ico_pic = true;//是否加图片水印
private $is_text = true;//是否加文字水印
private $src_x = 20;//水印在原图的x坐标
private $src_y = 20;//水印在原图的y坐标
private $ut = "utf-8";//文字编码
private $font_color = "#990000";//文字水印颜色
private $samll_pic_name = "smallpic";//小图的名称
private $big_pic_name = "bigpic";//大图的名称
function __construct($src_pic,$small_width,$small_height){
$this->checkfile($src_pic);
$this->checkfile($this->ico_pic);
$this->src_pic = $src_pic;
$this->small_width = $small_width;
$this->small_height = $small_height;
}private function __get($property_name){
return $this->$property_name;
}private function __set($property_name,$value){
return $this->$property_name = $value;
}
/**
* 取得图片的一些基本信息,类型为array
*/
function getimageinfo($image){
return @getimagesize($image);
}/**
* 把图片加载到php中
* $image 传进来的图片
*/
function getimage($image){
$image_info = $this->getimageinfo($image);
switch($image_info[2]){
case 1:
$img = @imagecreatefromgif($image);
break;
case 2:
$img = @imagecreatefromjpeg($image);
break;
case 3:
$img = @imagecreatefrompng($image);
break;
}
return $img;
}function createimageforsuffix($big_pic,$new_pic){
$image_info = $this->getimageinfo($this->src_pic);
switch($image_info[2]){
case 1:
//输出大图
@imagegif($big_pic,$this->big_pic_name.".gif");
//输出小图
@imagegif($new_pic,$this->samll_pic_name.".gif");
break;
case 2:
//输出大图
@imagejpeg($big_pic,$this->big_pic_name.".jpg");
//输出小图
@imagejpeg($new_pic,$this->samll_pic_name.".jpg");
break;
case 3:
//输出大图
@imagepng($big_pic,$this->big_pic_name.".png");
//输出小图
@imagepng($new_pic,$this->samll_pic_name.".png");
break;
}
}function checkfile($file){
if(!file_exists($file)){
die("图片:".$file."不存在!");
}
}function createsmallimage(){
$big_pic = $this->getimage($this->src_pic);
$big_pic_info = $this->getimageinfo($this->src_pic);
$new_pic = $this->getimage($this->ico_pic);
$new_pic_info = $this->getimageinfo($this->ico_pic);
$rgb = $this->convcolor();//判断是按宽比例缩放还是按高比例缩放
if($big_pic_info[0] > $big_pic_info[1]){
$ratio = $this->small_width/(int)$big_pic_info[0];
$small_pic_width = $this->small_width;
$small_pic_height = (int)($big_pic_info[1]*$ratio);
}else{
$ratio = $this->small_height/(int)$big_pic_info[1];
$small_pic_height = $this->small_height;
$small_pic_width = (int)($big_pic_info[0]*$ratio);
}//echo $small_pic_width = (int)($big_pic_info[0]*$ratio);
//echo $small_pic_height = (int)($big_pic_info[1]*$ratio);//是否打图片水印
if ($this->is_ico_pic){
//打图片水印
@imagecopy($big_pic,$new_pic,$this->src_x,$this->src_y,0,0,$new_pic_info[0],$new_pic_info[1]);
}
//是否打文字水印
if ($this->is_text){
//设置文字颜色
$text_color = @imagecolorallocate($big_pic,$rgb[0],$rgb[1],$rgb[2]);
//转换文字编码
$text = @iconv($this->ut,"utf-8",$this->ico_text);
//打文字水印
@imagettftext($big_pic,12,0,$this->src_x,$this->src_y,$text_color,"simkai_0.ttf",$text);
}
//新建一个新图片的画板
$new_pic = @imagecreatetruecolor($small_pic_width,$small_pic_height);
//生成缩略图
@imagecopyresized($new_pic,$big_pic,0,0,0,0,$small_pic_width,$small_pic_height,$big_pic_info[0],$big_pic_info[1]);
//输出图
$this->createimageforsuffix($big_pic,$new_pic);
}/**
* 类内部的功能函数把#000000转换成255,255,255
*/
private function convcolor(){
$rgb = array();
$color = preg_replace("/#/","",$this->font_color);
$c = hexdec($color);
$r = ($c >> 16) & 0xff;
$g = ($c >> 8) & 0xff;
$b = $c & 0xff;
$rgb[0] = $r;
$rgb[1] = $g;
$rgb[2] = $b;
return $rgb;
}
}
//调用方法
$pic = new smallpic("002.jpg",600,300);
$pic->is_text = true;
$pic->is_ico_pic = true;
$pic->ico_pic = "./images/004.png";
$pic->ico_text = "新年快乐!";
//$pic->src_x = 80;
$pic->src_y = 80;
$pic->ut = "utf-8";
$pic->font_color = "#0521f8";
$pic->samll_pic_name = "hslsamll";
$pic->big_pic_name = "hslbig";
$pic->createsmallimage();
?>
此程序实现php画柱状图它的设计思想是:首先以用imagecreate()来生成一个空白图形;其程序实现如下: //需要两种不同的颜色,正数一个 负数一个 ,一个数据数组,$path为保存图片的地址<?php教程
//此程序实现php画柱状图,开发股票网站的朋友经常会碰到在生成k线图片,现在我们来看看一款利用php根据数组数据生成k线图片。
//它的设计思想是:首先以用imagecreate()来生成一个空白图形;其程序实现如下:
//需要两种不同的颜色,正数一个 负数一个 ,一个数据数组,$path为保存图片的地址
$data = array ("-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24","-1174.58","-1865.41","1961.24");
$colors = array (array (255,0,0 ),array (0,255,0 ) );
$path = "gg15_600050.gif";
$title = "中国联通(600050)30日资金进出";
$date = array ("2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00","2010-05-20 00:00:00");
$hai = array(7203,6037,1931,9605,7405,8855,228,7771,8671,8791,1290,426,3265,4298,4809,3350,8204,8682,5523,7569,5853,593,3124,9062,3329,3236,2252,8756,2796,5164,2599,2630,1470,9779,8805,1512,1517,5635,760,1380,152,6035,2905,3163,9443,9432,5031,2838,6802,9330,1878,1333,4297,4180,3037,4527,9043,7287,3248,3448,3405,7204,2203,3974,4399,7333,4541,7033,4292,6128,9955,8603,297,6992,3282,9612,4483,8379,4016,6038,4163,3789,6896,8710,1502,1579,1712,3705,3619,5150,3007,1853,2776,3056,569,9031,6092,8600,8941,6366,5829,433,8011,9637,9106,1761,2422,9873,8186,1136,1763,3050,285,1962,775,9900,7371,2691,9051,6596,6858,6726,1527,3970,910,2621,7598,7899,6546,3678,2202,9411,6129,8352,4830,4484,6776,243,323,3946,751,7508,8173,3776,5071,3847,3318,4901,2102,7516,5251,3299,8859,2114,8711,9800,7707,5898,3489,580,5763,2470,4126,6269,5191,182,412,3043,3208,2438,6889,1732,1979,7692,3445,8214,8620,9446,4624,3615,5496,6152,213,3715,9623,1215,4757,9443,3366,2988,8043,6874,3742,4974,4812,6839,3016,5625,4656,5315,8303,8346,2315,3230,1969,1688,7381,6463,1030,8856,4691,6042,7652,8502,3839,7844,9149,4029,9465,2897,7688,3791,1458,6981,484,1290,4261,1222,9503,4881,7663,1399,2260,8066,6405,8442,722,9297,177,8426,2103,2078,3521,2280,8570,4663,6227,5784,7438,654,3036,9965,872,8306,3537,899,5888,1319,2472,9218,4853,7849,9433,1196,4880,1597,9258,7465,8138,817,1637,4201,1939,8914,1114,2232,8251,4538,7378,7438,4489,993,5269,944,2718,6633,4378,8431,155,5061,9347,1125,8865,4629,9965,4706,6263,8742,971,301);
draw(600,300,$data,$colors,$path,$title,$date);
function draw($w,$h,$data,$colors,$path,$title,$date)
{
//创建新空白图形
//箭头50像素,内容400*300
$width = $w;
$height = $h;
//取得左下角的坐标点,左边50像素和最下边50像素用于写标尺
$l_b_x = 50;
$l_b_y = $height/2;
$chartfont = 2; //图表字体
$font = 'c:/windows/fonts/simsun.ttc';//支持汉字的字体
$chartfontheight = imagefontheight($chartfont);//图表字体的大小
//取得最大的数据
$max = abs(max($data));
$max = $max>abs(min($data))?$max:abs(min($data));
$image = imagecreate($width,$height);
//填充背景为白色
$color_white = imagecolorallocate($image, 0xff, 0xff, 0xff);//白色
$color_diwen = imagecolorallocate($image,0xd8,0xd8,0xd8);//暗灰色
imagefill($image,0,0,$color_white);
//写入logo
imagettftext($image,20,0,$l_b_x+20,45,$color_diwen,$font,'华南财经网');
//画坐标
//坐标颜色为黑色,先画坐标,再画箭头
$black_color = imagecolorallocate($image,0x00,0x00,0x00);
imageline($image,$l_b_x,$l_b_y,$l_b_x,10,$black_color);
imageline($image,$l_b_x,$l_b_y,$width-10,$l_b_y,$black_color);
//箭头
imageline($image,$l_b_x,10,$l_b_x-3,14,$black_color);
imageline($image,$l_b_x,10,$l_b_x+3,14,$black_color);
imageline($image,$width-10,$l_b_y,$width-13,$l_b_y-3,$black_color);
imageline($image,$width-10,$l_b_y,$width-13,$l_b_y+3,$black_color);
//画底纹,灰色;画20条底纹;虚线
$pit = floor(($l_b_y-50)/10);//每一段的高度
$pit_x = floor(($width-$l_b_x-30)/count($data));//每一段的宽度
//计算单位
$x_max = $max;
$flag_danwei = 0;
while($x_max> 1000 )
{
$x_max /= 100;
$flag_danwei++;
}
switch($flag_danwei)
{
// case 0:$danwei = '(元)';break;
case 0:$danwei = '(万)';break;
case 1:$danwei = '(百万)';break;
case 2:$danwei = '(亿)';break;
case 3:$danwei = '(百亿)';break;
}
for($i=1;$i<11;$i++)
{
imageline($image,$l_b_x+1,$l_b_y-$i*$pit,$width-15,$l_b_y-$i*$pit,$color_diwen);
//从坐标的值
$v_v = number_format(($max/(10*pow(100,$flag_danwei))*$i),2);
$v_v = substr($v_v,0,5);
imagestring($image,
$chartfont,
$l_b_x-40,
$l_b_y-$i*$pit-10,
$v_v,
$black_color);
}
//原点坐标
imagestring($image,
$chartfont,
$l_b_x-20,
$l_b_y-10,
0,
$black_color);
//写入汉字,单位 和标题
imagettftext($image, 10,0,$l_b_x-45,20,$black_color,$font,$danwei);
//imagettftext($image, 11,0,$width/2-70,$l_b_y+20,$black_color, $font,$title);//画柱体,长方形,并着色
$color_big = imagecolorallocate($image,$colors[0][0],$colors[0][1],$colors[0][2]);
$color_small = imagecolorallocate($image,$colors[1][0],$colors[1][1],$colors[1][2]);
for($j=0;$j<count($data);$j++)
{
$d_h = floor(abs($data[$j]/$max*10*$pit));//柱体高度
$current_date = substr($date[$j],5,5);
// $current_date .= substr($date[$j],8,2);
imagerectangle($image,
$l_b_x+$j*$pit_x+1,
$l_b_y,
$l_b_x+($j+1)*$pit_x-2,
$l_b_y-$d_h,
$black_color);
//为矩形着色
$current_color = $data[$j]>0?$color_big:$color_small;
imagefilltoborder($image,
$l_b_x+$j*$pit_x+1 + floor($pit_x/2),
$l_b_y-floor($d_h/2),
$black_color,
$current_color);
//写入日期
/*imagestring($image,
$chartfont,
$l_b_x+$j*$pit_x,
$l_b_y+2,
$current_date,
$black_color);*/
imagettftext($image,8,90,$l_b_x+$j*$pit_x+10,$l_b_y-$d_h,$black_color,$font,$current_date);
}
if($path !="")
imagegif($image,$path);
else imagegif($image);
imagedestroy($image);
}//画图函数结束
//输出生成的图片
/*header("content-type: image/gif");
$my_colors[] = array(0xff,0x00,0x00);
$my_colors[] = array(0x00,0xff,0x00);
draw(600,400,array(1066565650,-40345340,50343433440,1834340,50343230,-8023454230,600,7232323300,-836),$my_colors,0,'教科书大阿斯顿即可');*/
//draw(600,400,array(1066,-4040,5040,1840,5030,-8230,600,7232,-836),$my_colors,0,'教科书大阿斯顿即可');
?>
本站原创教程,转载注明来源于http://www.111cn.net/phper/php.html 否则必究
一款php生成验证码实现代码,把自定义了session的目录,这样就不是php.ini里默认的session文件保存路径了,如下面session_save_path,可重新定义目录。session_save_path,可重新定义目录。
*/
$sesssavepath = dirname(__file__)."/../data/sessions/";
if(is_writeable($sesssavepath) && is_readable($sesssavepath)){ session_save_path($sesssavepath); }
session_start();
$vstr = '';
for($i=0; $i<4; $i++) $vstr .= chr(mt_rand(65,90));
if(function_exists("imagecreate")){
$ntime = time();
if(empty($_session['verifycode_last']) || empty($_session['verifycode']) || ($ntime - $_session['verifycode_last'] > 5)){
$_session['verifycode'] = strtolower($vstr);
$_session['verifycode_last'] = $ntime;
}
$vstr = $_session['verifycode'];
$vstrlen = strlen($vstr);
$img = imagecreate(50,20);
imagecolorallocate($img, 255,255,255);
$line1 = imagecolorallocate($img,240,220,180);
$line2 = imagecolorallocate($img,250,250,170);
for($j=3;$j<=16;$j=$j+3){
imageline($img,2,$j,48,$j,$line1);
}
for($j=2;$j<52;$j=$j+(mt_rand(3,6))){
imageline($img,$j,2,$j-6,18,$line2);
}
$bordercolor = imagecolorallocate($img, 0x99,0x99,0x99);
imagerectangle($img, 0, 0, 49, 19, $bordercolor);
$fontcolor = imagecolorallocate($img, 48,61,50);
for($i=0;$i<$vstrlen;$i++){
$bc = mt_rand(0,1);
$vstr[$i] = strtoupper($vstr[$i]);
imagestring($img, 5, $i*10+6, mt_rand(2,4), $vstr[$i], $fontcolor);
}header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");if(function_exists("imagejpeg")){
header("content-type:image/jpegrn");
imagejpeg($img);
}else{
header("content-type:image/pngrn");
imagepng($img);
}
imagedestroy($img);
exit();
}
1.用imagecreatetruecolor和imagecopyresampled函数分别取代imagecreate和imagecopyresized
2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$tofile,100))
imagecreatetruecolor -- 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int x_size, int y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像
*/
header ("content-type: image/png");
$im = @imagecreatetruecolor (50, 100)
or die ("cannot initialize new gd image stream");
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5, "a simple text string", $text_color);
imagepng ($im);
imagedestroy ($im);/*
如果使用普通的imagecreate()函数将造成图片质量失真的情况,从网上搜了一下解决办法,方法是用imagecreateruecolor()函数替换imagecreate()函数。
*/
function createpreview($img,$name,$path,$maxwidth,$maxheight,$quality){//图片,保存名称,保存路径,最大宽,最大高,质量
$widthratio=0;
$heightratio=0;
$width=imagesx($img);
$height=imagesy($img);
//开始计算缩小比例
if($width>$maxwidth||$height>$maxheight){
if($width>$maxwidth){
$widthratio=$maxwidth/$width;
}
if($height>$maxheight){
$heightratio=$maxheight/$height;
}
if($widthratio>0&&$heightratio>0){
if($widthratio<$heightratio){
$ratio=$widthratio;
}else{
$ratio=$heightratio;
}
}elseif($widthratio>0){
$ratio=$widthratio;
}elseif($heightratio>0){
$ratio=$heightratio;
}
//根据得出的比例,重新计算缩略图的宽和高
$newwidth=$ratio*$width;
$newheight=$ratio*$height;
$newimg=imagecreatetruecolor($newwidth,$newheight); // 创建目标图
imagecopyresized($newimg,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($newimg,$path."s_".$name,$quality);
imagedestroy($newimg);
}else{
imagejpeg($img,$path."s_".$name,$quality);
}
}
/*
imagecopyresamples() ,其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 imagecopyresized() 慢).
在php中给图片增加水印有imagecreatefromjpeg imagecreatefrompng imagecopymerge imagejpeg就成了,只要你设置原图与水印图片就成了,下面看实例。
*/
代码如下 | 复制代码 |
header("content-type: image/jpeg"); $filename='temp/www.111cn.net/111cn.net.jpg'; $im=imagecreatefromjpeg($filename); $s=imagecreatefrompng('mb.111cn.net/pic/water_template.png'); imagecopymerge($im,$s,0,200,0,0,365,27,20); imagejpeg($im); |
相关文章
js URLdecode()与urlencode方法支持中文解码
下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
- 这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)
最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24- 这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
- 在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
- 我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
- 一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
- 小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
- 共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
- PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10