php图像处理插件,ThinkPHP5图像处理插件
composer require topthink/think-image
前端处理
后台处理
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('image'
使用Composer安装ThinkPHP5的图像处理类库: composer require topthink/think-image 前端处理 后台处理 // 获取表单上传文件 例如上传了001.jpg $file = request()->file('image'); if (!$file) { die('未检测到文件上传'); } // 移动到框架应用根目录/uploads/ 目录下 $info = $file->move('uploads/image'); if ($info) { $oimage = $info->getFilename(); $nyr = date("Ymd"); $type = $info->getExtension(); // 裁剪图片 将图片裁剪为300x300并保存为crop.png $image = Image::open($file); $image->crop(300, 300)->save('uploads/caijian/' . $nyr . '/1' . $oimage, $type); $image = Image::open($file); $image->crop(300, 300, 100, 30)->save('uploads/caijian/' . $nyr . '/2' . $oimage, $type); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png $image = Image::open($file); $image->thumb(150, 150)->save('uploads/thumb/' . $nyr . '/' . $oimage, $type); // 居中裁剪 $image = Image::open($file); $image->thumb(150, 150, Image::THUMB_CENTER)->save('uploads/thumb/' . $nyr . '/1' . $oimage, $type); $image = Image::open($file); $image->thumb(150, 150, Image::THUMB_SOUTHEAST)->save('uploads/thumb/' . $nyr . '/2' . $oimage, $type); // 对图像进行以x轴进行翻转操作 $image = Image::open($file); $image->flip()->save('uploads/filp/' . $nyr . '/1' . $oimage, $type); $image = Image::open($file); $image->flip(Image::FLIP_Y)->save('uploads/filp/' . $nyr . '/2' . $oimage, $type); // 对图像使用默认的顺时针旋转90度操作 $image = Image::open($file); $image->rotate()->save('uploads/rotate/' . $nyr . '/1' . $oimage, $type); // 给原图左上角添加水印并保存water_image.png $waterimg = 'water.png'; $image = Image::open($file); $image->water($waterimg)->save('uploads/water/' . $nyr . '/1' . $oimage, $type); // 给原图左上角添加水印并保存water_image.png $image = Image::open($file); $image->water($waterimg, Image::WATER_NORTHWEST, 30)->save('uploads/water/' . $nyr . '/2' . $oimage, $type); // 给原图左上角添加水印并保存water_image.png $image = Image::open($file); $ttf = str_replace("\\", "/", request()->server()['DOCUMENT_ROOT']) . '/font/test.ttf'; $ttf2 = str_replace("\\", "/", request()->server()['DOCUMENT_ROOT']) . '/font/FZZHJW.TTF'; $image->text('my first', $ttf, 50, '#FFFFFF', 3)->text('文字水印', $ttf2, 50, '#FF0000', 9)->save('uploads/water/' . $nyr . '/3' . $oimage, $type); return json([ '扩展名' => $type, '带目录的' => $info->getSaveName(), '不带目录的' => $oimage, ]); } else { // 上传失败获取错误信息 return json([$file->getError()]); } 坑 1.插件代码save方法里没有判断目录 和创建目录 public function save($pathnamePHP图像处理, $type = null, $quality = 80, $interlace = true) { //自动获取图像类型 if (is_null($type)) { $type = $this->info['type']; } else { $type = strtolower($type); } //-------新增的修改-----------检测目录是否存在 $dirname = dirname($pathname); if ($dirname) { if (!file_exists($dirname)) { mkdir($dirname, 0777, true); } } //保存图像 if ('jpeg' == $type || 'jpg' == $type) { //JPEG图像设置隔行扫描 imageinterlace($this->im, $interlace); imagejpeg($this->im, $pathname, $quality); } elseif ('gif' == $type && !empty($this->gif)) { $this->gif->save($pathname); } elseif ('png' == $type) { //设定保存完整的 alpha 通道信息 imagesavealpha($this->im, true); //ImagePNG生成图像的质量范围从0到9的 imagepng($this->im, $pathname, min((int)($quality / 10), 9)); } else { $fun = 'image' . $type; $fun($this->im, $pathname); } return $this; } 2.文字水印文件大小 当字体文件过大 水印会加入失败 (编辑:海南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |