课程/PHP/后端开发
GD库实现图片水印与缩略图
-
-
黄枪枪
2017-03-18
- (下)
// 图片添加图片水印
public function imageMark($sourceMark,$local,$alpha){
$infoMark = getimagesize($sourceMark);
$typeMark = image_type_to_extension($infoMark[2],false);
$funMark = "imagecreatefrom{$type2}";
$water = $funMark($sourceMark);
imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$infoMark[0],$infoMark[1],$alpha);
imagedestroy($water);
}
//在浏览器中输出图片
public function show(){
header("Content-type:".$this->info['mime']);
$funs = "image{$this->info['type']}";
$funs($this->image);
}
//把图片保存在硬盘里
public function save($newname){
$funs = "image{$this->info['type']}";
$funs($this->image,$newname.'.'.$this->info['type']);
}
//销毁图片
public function __destruct(){
imagedestroy($this->image);
}
}
?>
PS: 此封装类,为完整封装
-
1赞 · 1采集
-
-
黄枪枪
2017-03-18
- (上)
<?php
class Image{
private $image; //内存中的图片
private $info; //图片的基本信息
//打开一张图片,读取到内存中
public function __construct($src){
$info = getimagesize($src);
$this->info=array(
'width'=>$info[0],
'height'=>$info[1],
'type'=>image_type_to_extension($info['2'],false),
'mime'=>$info['mime']
);
$fun = "imagecreatefrom{$this->info['type']}";
$this->image=$fun($src);
}
//按宽高值缩放图片
public function thumb($width,$height){
$image_thumb = imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
imagedestroy($this->image);//销毁原图像
$this->image = $image_thumb;//用压缩图片代替内存中原图像
}
// 图片添加文字水印
public function fontMark($content,$font_url,$size,$color,$local,$angle){
//设置字体颜色和透明度
$col = imagecolorallocatealpha($this->image,$color['R'],$color['G'],$color['B'],$color['A']);
//写入文字
imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
}
-
1赞 · 1采集
-
-
流浪佳人纳入怀
2016-10-29
- 图片水印方法的调用测试
-
截图
0赞 · 0采集
-
-
流浪佳人纳入怀
2016-10-29
- 图片水印方法封装
-
截图
0赞 · 1采集
-
-
小安Andrew
2016-10-19
- 图片水印封装
-
截图
0赞 · 0采集
-
-
phpcreate
2016-03-08
- 666
-
0赞 · 0采集
-
-
林静听蝉
2015-10-12
- image.class.php
//封装成类————添加图片水印(操作图片)
public function imageMark($source,$local,$alpha){
$info2 = getimagesize($source);
$type2 = image_type_to_extension($info2[2],false);
$fun2 = "imagecreatefrom{$type2}";
$water = $fun2($source);
imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
imagedestroy($water);
}
test.php
<?php
require "image.class.php";
$src = "yellow.jpg";
//添加图片水印
$source = 'weixin.png';
$local = array(
'x' => 20,
'y' => 50
);
$alpha = 30;
$image = new Image($src);
//$image->thumb(300,150);//压缩图片
//$image->fontMark($content,$font_url,$size,$color,$local,$angle);//文字水印
$image->imageMark($source,$local,$alpha);//图片水印
$image->show();
//$image->save("fontMark");//保存文字水印图片
?>
-
截图
0赞 · 0采集
-
-
常过无痕
2015-09-12
- 水印图的完善版,可以完美生成透明水印
/**
* 操作图片(图片水印)
* string $image_Mark 水印地址
* int $x x轴位置
* int $y y轴位置
* int $pct 合并程度 100为完全合并,无透明
*/
public function imageMark ($image_Mark,$x=0,$y=0,$pct=100)
{
$info2=getimagesize($image_Mark);
$width=$info2[0];//水印宽
$height=$info2[1];//水印高
$type=image_type_to_extension($info2[2],false);
$fun="imagecreatefrom{$type}";
$water=$fun($image_Mark);//创建水印图
if($pct==100){
imagecopy($this->image,$water,$x,$y,0,0,$width,$height);//无透明
}else{
//1.生成真彩图
$img = imagecreatetruecolor($width,$height);
//2.把原图画入画板
imagecopy($img,$this->image,0,0,$x,$y, $x+$width, $y+$height);
//3.在画板中加入水印
imagecopy($img,$water,0,0,0,0,$width,$height);
//4.把画板中的画改变透明度后画入原图,使得水印有了透明效果
imagecopymerge($this->image,$img,$x,$y,0,0, $width, $height,$pct);
imagedestroy($img);
}
imagedestroy($water);//释放水印图内存
}
-
2赞 · 3采集
-
-
小重山山232673
2015-09-12
- //保存图片
public function save($path){
$funs="image{$this->info['type']}";
$funs($this->image,$path.".".$this->info['type']);
}
//文字水印
public function fontMark($content,$url,$size,$color,$local,$angle){
$color=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$color,$url,$content);
}
//图片水印
public function imageMark($source,$local,$alpha){
$info2=getimagesize($source);
$type2=image_type_to_extension($info2[2],false);
$func2="imagecreatefrom{$type2}";
$water=$func2($source);
imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
imagedestroy($water);
}
//销毁图片
public function __destruct(){
imagedestroy($this->image);
}
-
0赞 · 0采集
-
-
小重山山232673
2015-09-12
- <?php
class Image{
public $info;
private $image;
//构造函数,打开文档
public function __construct($src){
$info=getimagesize($src);
$this->info=array(
'width'=>$info[0],
'height'=>$info[1],
'type'=>image_type_to_extension($info[2],false),
'mime'=>$info['mime']
);
$fun="imagecreatefrom{$this->info['type']}";
$this->image=$fun($src);
}
//压缩图片
public function thumb($width,$height){
$image_thumb=imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
imagedestroy($this->image);
$this->image=$image_thumb;
}
//显示 图片
public function show(){
header("Content-type:".$this->info['mime']);
$funs="image{$this->info['type']}";
$funs($this->image);
}
}
?>
-
0赞 · 1采集
-
-
环境中改变
2015-06-14
- 我全部看完,也跟着做完了!
-
0赞 · 1采集