fastadmin上传图片并压缩并加水印

admin5个月前Thinkphp195
   public function image()
    { //        $url = 'uploads/20241026/8f32aebb8be904c5c01ea80de534880e.png';  $file = request()->file('file'); Db::startTrans(); try {
            $upload = new Upload($file);
            $attachment = $upload->upload();
            $url = substr($attachment->url, 1);
            $image_info = getimagesize($url);
            $params = array( 'admin_id' => 0, 'user_id' => (int)$this->user_id, 'filename' => '', 'category' => 0, 'filesize' => $image_info['bits'], 'imagewidth' => $image_info['0'], 'imageheight' => $image_info['1'], 'imagetype' => $image_info['mime'], 'imageframes' => 0, 'mimetype' => $image_info['mime'], 'drawing' => '/' . $url, 'uploadtime' => time(), 'createtime' => time(), 'updatetime' => time(), 'storage' => 'local', 'sha1' => '', 'extparam' => '',
            ); if ($image_info['mime'] != 'image/png' && $image_info['mime'] != 'image/jpeg') { Db::rollback();
                $this->error('类型为pngjpg');
            }
            $path = "uploads_copy/" . date("Ymd"); if (!is_dir($path)) { //判断目录是否存在 不存在就创建  mkdir($path, 0777, true); chmod($path, 0777);
            }
            $img = $url; list($width, $height, $img_type, $attr) = @getimagesize($img);
            $imageinfo = array('width' => $width, 'height' => $height, 'type' => image_type_to_extension($img_type, false), 'attr' => $attr);
            $fun = "imagecreatefrom" . $imageinfo['type'];
            $image = $fun($img);
            $w_image = $fun($img);
            $percent = '0.5';
            $new_width = $imageinfo['width'] * $percent;
            $new_height = $imageinfo['height'] * $percent;
            $image_thump = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $imageinfo['width'], $imageinfo['height']);
            $image = $image_thump;
            $func = 'image' . $imageinfo['type'];
            $title = str_replace('uploads', 'uploads_copy', $img);
            $func($image, $title); // 添加水印  $fontPath = '/www/wwwroot/ai_end.qiuyutv.com/public/cert/ttf.ttf';
            $text = '秋娱Ai';
            $color = imagecolorallocatealpha($w_image, 255, 255, 255, 50); 
 // 白色透明度50  $fontSize = 40; 
 // 字体大小  $angle = 0; 
 // 倾斜角度  $x = 20; // X坐标  $y = 100; // Y坐标  imagettftext($w_image, $fontSize, $angle, $x, $y, $color, $fontPath, $text); imagejpeg($w_image, $title); imagedestroy($image);
            $params['url'] = '/' . $title;
            $msg = Db::name('attachment')->insertGetId($params); Db::commit();
        } catch (\Exception $e) { Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'm_id' => $msg, 'fullurl' => cdnurl($attachment->url, true)]);
    }

相关文章

关注公众号登录

/* * 服务端生成带唯一标识二维码并将唯一标识返回给前端 */ public function wx_code() { $scene_str...

文章中图片路径替换

if ($list['post_content']) { $list['post_content'] = html_entity_decode($list['post_content']);...

微信公众号h5登录

public function getCode(){    $order_id = '95896766';    $config = confi...

thinkphp 加cache锁

$lockKey = 'user_lock_requirement' . $this->user_id; $isLocked = Cache::get($lockKey); if ($isLo...

查找重复的数据

$data = Db::name('about_product_list')->field('id,title,count(id) as num')->gr...

微信卡卷html 领取的签名

/* * 创建签名 */public function sign(){    $cardId = 'p7YU46qidv5_baigH0Ds4ObP7MDg'; &...