PHP programming Kindeditor upload image and watermark implementation

Source: Internet
Author: User
Tags php programming

For how to add watermark on the picture is a difficult problem that many technical personnel encounter, that everyone has seen the public number and some other technology platform can realize this function, but how to realize the source code, but there is no clue, then today to introduce you about Kindeditor upload pictures and watermark method, After reading, I believe that the technical staff will know.
First step: Modify the upload_json.php file
This file can be found in the/php/directory of the editor and a new function is added:
/*

  • Features: PHP image watermark, watermark support picture or text
  • Parameters:
  • $groundImage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
  • $waterPos watermark position, there are 10 kinds of states, 0 is random position;
  • 1 for the top of the left, 2 for the top center, 3 for the top right;
  • 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
  • 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
  • $waterImage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
  • $alpha watermark transparency, value 1-100;
  • $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
  • $textFont text size with a value of 1, 2, 3, 4, or 5, which defaults to 5;
  • $textColor the text color, the value is the hexadecimal color value, the default is #ff0000 (red);
  • $waterImage and $waterText better not use at the same time, choose one of them, priority to use the $waterImage.
  • When the $waterimage is valid, the parameters $waterstring, $stringFont, $stringColor do not take effect.
  • The watermark image has the same file name as the $groundImage.
    */
    function Imagewatermark ($groundImage, $waterPos =0, $waterImage = ", $alpha =80, $waterText =", $water _fontfile, $ textfont=9, $textColor = ' #FF0000 ') {
    $isWaterImage = FALSE;
    $FORMATMSG = ' The picture format is not supported! Use a picture in GIF, JPG, PNG format. ‘;
    $fontFile = $water _fontfile;
    Read Watermark File
    if (!empty ($waterImage) && file_exists ($waterImage)) {
    $isWaterImage = TRUE;
    $water _info = getimagesize ($waterImage);
    $water _w = $water _info[0];//To obtain the width of the watermark picture
    $water _h = $water _info[1];//To obtain a high watermark image
    Switch ($water _info[2]) {//Get the format of the watermark picture
    Case 1: $water _im = Imagecreatefromgif ($waterImage);
    Case 2: $water _im = Imagecreatefromjpeg ($waterImage);
    Case 3: $water _im = Imagecreatefrompng ($waterImage);
    Default:die ($FORMATMSG);
    }
    }
    Read 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 a high background picture
    Switch ($ground _info[2]) {//Get the format of the background picture
    Case 1: $ground _im = Imagecreatefromgif ($groundImage);
    Case 2: $ground _im = Imagecreatefromjpeg ($groundImage);
    Case 3: $ground _im = Imagecreatefrompng ($groundImage);
    Default:die ($FORMATMSG);
    }
    }else{
    Alert ("Watermark picture does not exist!") ");
    }
    Watermark Location
    if ($isWaterImage) {//Picture watermark
    $w = $water _w;
    $h = $water _h;
    $label = "Picture";
    }else{//text watermark
    $temp = Imagettfbbox ($textFont, 0, $fontFile, $waterText);//Gets the range of text that uses TrueType fonts
    $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 picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ";
    Return
    }
    Switch ($waterPos) {
    Case 0://Random
    $posX = rand (0, ($ground _w-$w));
    $posY = rand (0, ($ground _h-$h));
    Break
    Case 1://1 for top left
    $posX = 0;
    $posY = 0;
    Break
    Case 2://2 centered on the top
    $posX = ($ground _w-$w)/2;
    $posY = 0;
    Break
    Case 3://3 for top right
    $posX = $ground _w-$w;
    $posY = 0;
    Break
    Case 4://4 for the middle left
    $posX = 0;
    $posY = ($ground _h-$h)/2;
    Break
    Case 5://5 centered in the middle
    $posX = ($ground _w-$w)/2;
    $posY = ($ground _h-$h)/2;
    Break
    Case 6://6 for the middle right
    $posX = $ground _w-$w;
    $posY = ($ground _h-$h)/2;
    Break
    Case 7://7 for Bottom left
    $posX = 0;
    $posY = $ground _h-$h;
    Break
    Case 8://8 centered at bottom
    $posX = ($ground _w-$w)/2;
    $posY = $ground _h-$h;
    Break
    Case 9://9 for bottom 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
    }
    To set the color blending mode of an image
    Imagealphablending ($ground _im, true);
    if ($isWaterImage) {//Picture watermark
    Imagecopy ($ground _im, $water _im, $posX, $posY, 0, 0, $water _w, $water _h);//copy watermark to target file
    Generating 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,));
    $G = Hexdec (substr ($textColor, 3,2));
    $B = Hexdec (substr ($textColor, 5));
    } else {
    Die ("Watermark text color format is not correct! ");
    }
    Imagestring ($ground _im, $textFont, $posX, $posY, $waterText, Imagecolorallocate ($ground _im, $R, $G, $B)); br/>}
    Create a picture after a watermark
    @unlink ($groundImage);
    Case 1:imagegif ($ground _im, $groundImage);
    Case 2:imagejpeg ($ground _im, $groundImage);
    Case 3:imagepng ($ground _im, $groundImage);
    Default:die ($ERRORMSG);
    }
    Freeing memory
    if (Isset ($water _info)) unset ($water _info);
    if (Isset ($water _im)) Imagedestroy ($water _im);
    unset ($ground _info);
    Imagedestroy ($ground _im);
    }

Step Two: Find $json = new Services_json (); note there are two places, not the one in the alert function, add the following code:
/ watermark Configuration Start /
$water _mark = 1;//1 Watermark, the other is not added
$water _pos = 9;//watermark position, 10 states "0 for random, 1 for the top left, 2 for the top, 3 for the top right, 4 for the middle, 5 for the middle, 6 for the middle, 7 for the bottom, and the lower for 8 for the bottom."
$water _img = $_server[' Document_root '). ' /upfiles/water.gif ';//watermark picture, the default fill empty, please upload the picture to the site root directory upfiles, example: Water.gif
$water _alpha = 50;//watermark Transparency
$water _text = ";//watermark string, default fill empty;
$water _fontfile = $_server[' Document_root '). ' /upfiles/fonts/arial.ttf ';//font used for text watermark;
if ($water _mark = = 1) {
Imagewatermark ($file _path, $water _pos, $water _img, $water _alpha, $water _text, $water _fontfile);
}
/ watermark configuration End /

The above is the implementation of the code, then this method is mainly applicable to PHP programming, other languages you can digest, if there is no understanding of the place, you can leave a message under discussion.

This article by the Professional app development Brigitte Xuan Science and technology finishing release, if need reprint please indicate the original author and source!

PHP programming Kindeditor upload image and watermark implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.