SoFunction
Updated on 2025-03-10

Kindeditor editor adds image upload watermark function (php code)

KindEditor is an open source online HTML editor. Developers can use KindEditor to replace traditional multi-line text input boxes with visual rich text input boxes. It can be seamlessly integrated with Java, .NET, PHP, ASP and other programs, and is more suitable for use on Internet applications such as CMS, malls, forums, blogs, Wikis, and emails.
Main features:

Fast: Small size, fast loading speed

Open source: open source, high level, high quality

Bottom level: Built-in custom DOM class library, precise operation of DOM

Extension: Based on plug-in design, all functions are plug-ins, and functions can be added and reduced according to requirements

Style: It's very easy to modify the editor style, just modify a CSS file

Compatibility: Supports most mainstream browsers, such as IE, Firefox, Safari, Chrome, Opera

Go to the topic. When uploading pictures in the default editor, there is no watermark function. The following is a detailed introduction:

first step:Modify upload_json.php file

This file can be found in the /php/ directory of the editor, and a new function is added:

/*
   * Function: PHP image watermark, watermark supports pictures or text
   * Parameters:
   * $groundImage background image, that is, images that need to be watermarked, only support GIF, JPG, and PNG formats;
   * $waterPos The watermark position has 10 states, and 0 is a random position;
   * 1 is the top on the left, 2 is the top on the center, and 3 is the top on the right;
   * 4 is the center on the left, 5 is the center on the middle, and 6 is the center on the right;
   * 7 is the bottom end on the left, 8 is the bottom end in the center, and 9 is the bottom end on the right;
   * $waterImage image watermark, that is, the image as a watermark, only supports GIF, JPG, and PNG formats;
   * $alpha watermark transparency, value 1-100;
   * $waterText Text watermark, that is, text is used as a watermark, supports ASCII code, and does not support Chinese;
   * $textFont Text size, value is 1, 2, 3, 4, or 5, default is 5;
   * $textColor Text color, value is hexadecimal color value, default is #FF0000 (red);
   *
   * It is best not to use $waterImage and $waterText at the same time. Just select one of them and use $waterImage first.
   * When $waterImage is valid, the parameters $waterString, $stringFont, and $stringColor are not valid.
   * The file name of the image after watermarking is the same as $groundImage.
 */
function imageWaterMark($groundImage, $waterPos=0, $waterImage='', $alpha=80, $waterText='', $water_fontfile, $textFont=9, $textColor='#FF0000'){
  $isWaterImage = FALSE;
  $formatMsg = 'This image format is not supported!  Please use GIF, JPG, and PNG format pictures.  ';
  $fontFile = $water_fontfile;
  //Read the watermark file  if(!empty($waterImage) && file_exists($waterImage)){
    $isWaterImage = TRUE;
    $water_info = getimagesize($waterImage);
    $water_w = $water_info[0];//Get the width of the watermark image    $water_h = $water_info[1];//Get the high of the watermark image    switch($water_info[2]){//Get the format of the watermark image      case 1:$water_im = imagecreatefromgif($waterImage);break;
      case 2:$water_im = imagecreatefromjpeg($waterImage);break;
      case 3:$water_im = imagecreatefrompng($waterImage);break;
      default:die($formatMsg);
    }
  }
  //Read the background picture  if(!empty($groundImage) && file_exists($groundImage)){
    $ground_info = getimagesize($groundImage);
    $ground_w = $ground_info[0];//Get the width of the background image    $ground_h = $ground_info[1];//Get the background image high    switch($ground_info[2]){//Get the background image format      case 1:$ground_im = imagecreatefromgif($groundImage);break;
      case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
      case 3:$ground_im = imagecreatefrompng($groundImage);break;
      default:die($formatMsg);
    }
  }else{
    alert("The watermark picture does not exist!");
  }
  //Watermark location  if($isWaterImage){//Picture watermark    $w = $water_w;
    $h = $water_h;
    $label = "Pictures";
  }else{// Text watermark    $temp = imagettfbbox($textFont, 0, $fontFile, $waterText);//Get the range of text using TrueType font    $w = $temp[2] - $temp[6];
    $h = $temp[3] - $temp[7];
    unset($temp);
    $label = "Text area";
  }
  if(($ground_w<$w) || ($ground_h<$h)){
    echo "The length or width of the image that needs to be watermarked is greater than the watermark".$label."It's still small, and it's impossible to generate a watermark!";
    return;
  }
  switch($waterPos){
    case 0://random    $posX = rand(0,($ground_w - $w));
    $posY = rand(0,($ground_h - $h));
    break;
    case 1://1 is the top and the left    $posX = 0;
    $posY = 0;
    break;
    case 2://2 is the top center    $posX = ($ground_w - $w) / 2;
    $posY = 0;
    break;
    case 3://3 is the top and the right    $posX = $ground_w - $w;
    $posY = 0;
    break;
    case 4://4 is the center on the left    $posX = 0;
    $posY = ($ground_h - $h) / 2;
    break;
    case 5://5 is the middle part in the middle    $posX = ($ground_w - $w) / 2;
    $posY = ($ground_h - $h) / 2;
    break;
    case 6://6 is the center on the right    $posX = $ground_w - $w;
    $posY = ($ground_h - $h) / 2;
    break;
    case 7://7 is the bottom and the left    $posX = 0;
    $posY = $ground_h - $h;
    break;
    case 8://8 is the bottom end in the center    $posX = ($ground_w - $w) / 2;
    $posY = $ground_h - $h;
    break;
    case 9://9 is the bottom and the right    $posX = $ground_w - $w;
    $posY = $ground_h - $h;
    if(!$isWaterImage){
    $posY = $ground_h - $h-20;
    }
    break;
    default://random    $posX = rand(0,($ground_w - $w));
    $posY = rand(0,($ground_h - $h));
    break;
  }
  //Set the image color mixing mode  imagealphablending($ground_im, true);
  if($isWaterImage){//Picture watermark    //imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h);//Copy the watermark to the target file    //Generate mixed images    imagecopymerge($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h, $alpha);
  } else {// Text watermark    if( !empty($textColor) && (strlen($textColor)==7)){
      $R = hexdec(substr($textColor,1,2));
      $G = hexdec(substr($textColor,3,2));
      $B = hexdec(substr($textColor,5));
    } else {
      die("The color format of the watermark text is incorrect!");
    }
    imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  }
  //The image after generating a watermark  @unlink($groundImage);
  switch($ground_info[2]){//Get the background image format    case 1:imagegif($ground_im,$groundImage);break;
    case 2:imagejpeg($ground_im,$groundImage,100);break;//Note that 100 here. After testing, 100 has the best image quality, but the file size will increase a lot. The quality at 95 is not bad, and the size is similar to the original one.  The author uses a value of 95.    case 3:imagepng($ground_im,$groundImage);break;
    default:die($errorMsg);
  }
  //Release the memory  if(isset($water_info)) unset($water_info);
  if(isset($water_im)) imagedestroy($water_im);
  unset($ground_info);
  imagedestroy($ground_im);
}

Step 2:Find $json = new Services_JSON(); Note that there are two places, not the one in the alert function, add the following code:

 /* Watermark configuration starts */
  $water_mark = 1;//1 is a watermark, others are not added  $water_pos = 9;//Watermark position, 10 states [0 is random, 1 is the top on the left, 2 is the top on the center, 3 is the top on the right; 4 is the middle on the left, 5 is the middle on the center, 6 is the middle on the right; 7 is the bottom on the left, 8 is the bottom on the center, 9 is the bottom on the center]  $water_img = $_SERVER['DOCUMENT_ROOT'].'/upfiles/';//Watermark image, fill in empty by default. Please upload the image to the upfiles in the root directory of the website, example:  $water_alpha = 50;//Watermark transparency  $water_text = '';//Watermark string, fill in blank by default;  //$water_fontfile = $_SERVER['DOCUMENT_ROOT'] .'/upfiles/fonts/';//Fonts used for text watermarks;  if($water_mark == 1){
    imageWaterMark($file_path, $water_pos, $water_img, $water_alpha, $water_text, $water_fontfile);
  }
  /* The watermark configuration ends */

It can be used normally after my tests. Please pay attention to the path of the watermark image, depending on the actual situation.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.