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('类型为png或jpg');
}
$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)]);
}