当前位置: 代码迷 >> 综合 >> laravel8 文件上传 缩略图剪切 图片水印
  详细解决方案

laravel8 文件上传 缩略图剪切 图片水印

热度:14   发布时间:2023-10-20 16:11:57.0

首先需要使用 composer 在命令行安装最新版本的 intervention/image :

composer require intervention/image

控制器

    //图片缩略 100*100 水印header("Content-Type:text/html;charset=UTF-8");$file=$request->file('file');$filename=time().'.'.$file->getClientOriginalExtension();//压缩图片//引入的类 use Intervention\Image\Facades\Image;$img=Image::make($file)->resize(100,100);$image=$img->save('uploads/'.$filename);$images=$image->basename;print_r($images);
//添加水印$img->insert('http://www.mon.com/upload/12345.png','bottom-right',15,10);
  $html=file_get_contents('https://n.eastday.com/pnews/162337384677010918');$rules=['title'=>['h1','text'],'author'=>['.author','text'],'img'=>['img','src'],'talk'=>['p','text']];$data=QueryList::html($html)->rules($rules)->range('.mainBox')->query()->getData();$result=$data->all();//伪静态foreach ($result as $k=>$v){
    $name=file_get_contents($v['img']);$filename="./uploads/1".$k.".jpg";file_put_contents($filename,$name);$filename=substr($filename,1);}$data=Messmodel::add($result);if ($data){
    echo "采集并添加入库成功";}else{
    echo "采集失败";}
单例连接数据库
<?php
class Mysql{
    private static $mysql=null;private function __clone(){
    // TODO: Implement __clone() method.}private function __construct(){
    }//入口public static function concent(){
    if (self::$mysql===null){
    try {
    self::$mysql=mysqli_connect('127.0.0.1','root','123456','zg5');}catch(\Exception $e){
    return ['连接失败'.$e->getMessage()];}}if (!self::$mysql instanceof self){
    try {
    self::$mysql=mysqli_connect('127.0.0.1','root','123456','zg5');}catch(\Exception $e){
    return ['连接失败'.$e->getMessage()];}}return  self::$mysql;}
}var_dump($obj=Mysql::concent('127.0.0.1','root','123456','zg5'));