PHP programming kindeditor upload image watermark implementation

  How to add a watermark to an image is a difficult problem encountered by many technicians. Everyone has seen that WeChat public accounts and some other technical platforms can implement this function, but they have no clue as to how the source code is implemented. Today, I will introduce to you how to add watermarks to pictures uploaded by kindeditor. After reading it, I believe that all technicians will know it.

Step 1: Modify the upload_json.php file . You can find this file in the /php/ directory of
the editor and add a function:

/*

    * Function: PHP image watermark, watermark supports image or text

    * parameters:

    * $groundImage background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, PNG format;

    * $waterPos watermark position, there are 10 states, 0 is a random position;

    * 1 is the top left, 2 is the top center, 3 is the top right;

    * 4 is the middle left, 5 is the middle, 6 is the middle right;

    * 7 is the bottom left, 8 is the bottom center, 9 is the bottom right;

    * $waterImage image watermark, that is, the image used as a watermark, currently only supports GIF, JPG, PNG format;

    * $alpha watermark transparency, value 1-100 ;

    * $waterText text watermark, that is, use the text as a watermark, support ASCII code, do not support Chinese;

    * $textFont text size, the value is 1 , 2 , 3 , 4 or 5 , the default is 5 ;

    * $textColor text color, the value is a hexadecimal color value, the default is #FF0000 ( red ) ;

    *

    * It is best not to use $waterImage and $waterText at the same time, choose one of them, and use $waterImage first .

    * When $waterImage is valid, the parameters $waterString , $stringFont and $stringColor are invalid .

    * The file name of the watermarked image is the same as $groundImage .

*/

function imageWaterMark($groundImage, $waterPos=0, $waterImage='', $alpha=80, $waterText='', $water_fontfile, $textFont=9, $textColor='#FF0000'){

    $isWaterImage = FALSE;

    $formatMsg = ' The image format is not supported! Please use images in GIF , JPG , PNG format. ';

    $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 height 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 background image

    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 height of the background image

        switch($ground_info[2]){// Get the format of the background image

            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 image does not exist! ");

    }

    // watermark position

    if($isWaterImage){// Image watermark

        $w = $water_w;

        $h = $water_h;

        $label = " Image's ";

    }else{// Text watermark

        $temp = imagettfbbox($textFont, 0, $fontFile, $waterText);// Get the range of text using 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 image that needs to be watermarked is smaller than the watermark ".$label." , so the watermark cannot be generated! ";

        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 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 right

        $posX = $ground_w - $w;

        $posY = 0;

        break;

        case 4://4 is left in the middle

        $posX = 0;

        $posY = ($ground_h - $h) / 2;

        break;

        case 5://5 is centered in the middle

        $posX = ($ground_w - $w) / 2;

        $posY = ($ground_h - $h) / 2;

        break;

        case 6://6 is right in the middle

        $posX = $ground_w - $w;

        $posY = ($ground_h - $h) / 2;

        break;

        case 7://7 is the bottom left

        $posX = 0;

        $posY = $ground_h - $h;

        break;

        case 8://8 is the bottom center

        $posX = ($ground_w - $w) / 2;

        $posY = $ground_h - $h;

        break;

        case 9://9 is the 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;

    }

    // Set the color mixing mode of the image

    imagealphablending($ground_im, true);

    if($isWaterImage){// Image watermark

        //imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h);// Copy the watermark to the target file

        // generate mixed image

        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 watermark text color format is incorrect! ");

        }

        imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));

    }

    // Generate the image after the watermark

    @unlink($groundImage);

    switch($ground_info[2]){// Get the format of the background image

        case 1:imagegif($ground_im,$groundImage);break;

        case 2:imagejpeg($ground_im,$groundImage);break;

        case 3:imagepng($ground_im,$groundImage);break;

        default:die($errorMsg);

    }

    // release 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 to add watermark, others are not to add

    $water_pos = 9;//Watermark position, 10 states [0 is random, 1 is top left, 2 is top center, 3 is top right; 4 is middle left, 5 is middle center, 6 is middle right; 7 is the bottom left, 8 is the bottom center, and 9 is the bottom.]

    $water_img = $_SERVER['DOCUMENT_ROOT'].'/upfiles/water.gif';//Watermark image, fill in blank by default, please upload the image to the upfiles in the root directory of the website, for example: water.gif

    $water_alpha = 50;//Watermark transparency

    $water_text = '';//Watermark string, empty by default;

    //$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);

    }

    /* End of watermark configuration */

  The above is the implementation of the code, then this method is mainly applicable to php programming, and other languages ​​can be mastered. If there is still something you don't understand, you can leave a message below for discussion.

  This article is organized and published by Yixuan Technology, a professional app developer. If you need to reprint, please indicate the original author and source!

Guess you like

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