Thinkphp upload image and add image watermark

  1. Description: Thinkphp adds a text watermark in the upper left corner. According to thinkphp3.2 development document, Portal: Click to enter .

Use Think\Image class for image processing functions, support Gd library and Imagick library, including support for GIf image processing.

  2. The steps are as follows: instantiate the class library - "open the image file -" operate the image

  3. Code

public function aa(){
        $info = "./Public/images/3.jpg";
        $newinfo = $this->imgtowm($info);
        dump($newinfo);
    }
    //Add image watermark to image
    public function imgtowm($ImgPath= null){
        $image = new \Think\Image();
        if(!$ImgPath){
            return "image path does not exist";
        }
        //logo location
        $ImgWaterPath = "./Public/images/logo.jpg";
        //Generate image naming rules
        $NewImgName = time();
        //Generate image save path
        $NewImgPath = "./Public/Uploads/watermark/".$NewImgName.".jpg";
        $image->open("$ImgPath")->water($ImgWaterPath,\Think\Image::IMAGE_WATER_NORTHWEST)->save($NewImgPath);
        //Check if the file exists
        if(file_exists($NewImgPath)) {
            // delete the original image
            //unlink($ImgPath);
            $ data = $ NewImgPath;
        }else{
            $data = "The file ".$NewImgName." does not exist in the current directory";
        }
        return $data;
    }

  4. Effect preview

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325000686&siteId=291194637