PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

利用php GD库 文字图片水印 缩略图

原创
2016-06-08 17:20:21 750浏览

GD库是php中一个默认的强大的图片处理库了,我们可以利用它来对图片进行一些操作或生成图片的操作,下面我们来看文字图片水印缩略图在php中的实例。

一:添加文字水印 使用方法

require 'image.class.php'
$src="//m.sbmmt.com/m/faq/001.jpg";
$content="hello";
$font_url="my.ttf";
$size=20;
$image=new Image($src);
$color=array(
0=>255,
1=>255,
2=>255,
2=>20
);
$local=array(
'x'=>20,
'y'=>30
);
$angle=10;
$image->fontMark($content,$font_url,$size,$color,$local,$angle);
$image->show();


二:图片缩略图 使用方法:


require 'image.class.php'
$src="//m.sbmmt.com/m/faq/001.jpg";
$image=new Image($src);
$image->thumb(300,200);
$image->show();


三:image.class.php

class image{
private $info;
private $image;
public function __contruct($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 thumd($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[0],$color[1],$color[2],$color[3]);
$text=imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
}
//输出图片
public function show()
{
header("Content-type:",$this->info['mime']);

$func="image{$this->info['type']}";
$func($this->image);

}
public function save($nwename){
$func="image{$this->info['type']}";
//从内存中取出图片显示
$func($this->image);
//保存图片
$func($this->image,$nwename.$this->info['type']);

}
public function _destruct(){

imagedestroy($this->image);
}

}

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。