The following is a class I provided, which encapsulates these three functions:
<?php /** * */ class ImageHelper { private $imgSrc; private $info; private $type; private $image; private $showOrSaveFunc; public function __construct($imgSrc) { $this->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); } } ?>
<?php require_once 'ImageHelper.class.php'; $imageHelper = new ImageHelper('P60219-211631.jpg'); /*$imageHelper->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(); ?>
The above introduces how to create image thumbnails, add watermarks, and add fonts with PHP, including image creation and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.