您好,欢迎访问一九零五行业门户网

PHP制作图片缩略图、加水印、加字体 php网站制作教程 php网站后台制作教程 phpcms模板制

下面是我提供的一个类,下面封装了这三种功能:
imgsrc = $imgsrc; $this->init(); } /** * 初始化操作 */ private function init() { //获取图片信息(通过gd库提供的方法,得到你想要处理的图片的基本信息) $this->info = getimagesize($this->imgsrc); //通过图像的编号来获取图像的类型 $this->type = image_type_to_extension($this->info[2], false); //在内存中创建一个和我们图像类型一样的图像 $fun = imagecreatefrom{$this->type}; //把图片复制到内存中 $this->image = $fun($this->imgsrc); $this->showorsavefunc = image{$this->type}; } /** * 给图片添加字体 * @param $fontfile 字体文件路径 * @param $text 文字内容 * @param $red 红色分量 * @param $green 绿色分量 * @param $blue 蓝色分量 * @param $alpha 透明度 * @param $angle 偏移角度 * @param $size 字体大小 * @param $x 距离左边的距离 * @param $y 距离右边的距离 */ public function addfont($fontfile, $text, $red, $green, $blue, $alpha , $angle, $size, $x, $y) { $color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha); imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text); } /** * 浏览器输出图片类型 */ private function setoutputheader() { header(content-type:.$this->info['mime']); } /** * 输出图片到浏览器 */ public function outputimagetobrowser() { $this->setoutputheader(); $this->excshoworsavefunc($this->showorsavefunc); } /** * 输出或者保存图片 */ private function excshoworsavefunc($type, $file='') { if ($file == '') { $type($this->image); } else { $type($this->image, $file); } } /** * 保存文件到本地 * @param 本地路径 * @param 文件名 */ public function outputimagetostorage($filepath, $filename) { $this->setoutputheader(); $this->excshoworsavefunc($this->showorsavefunc, $filepath.$filename.'.'.$this->type); } /** * 为图片添加水印 * @param $watermarkpath 水印图片路径 * @param $dst_x 水印距离原图左侧的距离 * @param $dst_y 水印距离原图上侧的距离 * @param $pct 水印的透明度 */ public function addwatermark($watermarkpath, $dst_x, $dst_y, $pct) { $waterinfo = getimagesize($watermarkpath); $watertype = image_type_to_extension($waterinfo[2], false); $waterfun = imagecreatefrom{$watertype}; $watermarkimage = $waterfun($watermarkpath); //imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct) //pct透明度 imagecopymerge($this->image, $watermarkimage, $dst_x, $dst_y, 0, 0, $waterinfo[0], $waterinfo[1], $pct); imagedestroy($watermarkimage); } /** * 生成图片缩略图 * @param $mwidth 想要压缩的宽度 */ public function createthumbnail($mwidth) { $width = $this->info[0]; $height = $this->info[1]; if ($mwidth >= $width) { return ; } $mheight = $height * $mwidth / $width; //1.在内存中建立一个宽300、高200的真色彩图片 $imagethumb = imagecreatetruecolor($mwidth, $mheight); //2.核心步骤、将原图复制到新建的正色彩图片上,并按照一定比例压缩 //imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h) imagecopyresampled($imagethumb, $this->image, 0, 0, 0, 0, $mwidth, $mheight, $width, $height); $this->image = $imagethumb; } /* * 销毁图片资源 */ public function destroy() { imagedestroy($this->image); } } ?>
使用方法如下:
addfont(consola.ttf, raid, 0, 0, 0, 50 , 0, 45, 20, 20); $imagehelper->addwatermark('pic2.jpg', 0, 0, 50);*/ $imagehelper->createthumbnail(300); // $imagehelper->outputimagetobrowser(); $imagehelper->outputimagetostorage('','testimagehelperaddfont'); $imagehelper->destroy(); ?>
以上就介绍了php制作图片缩略图、加水印、加字体,包括了制作图片,php方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息