Form:http://www.uphtm.com/php/255.html
Ueditor Editor upload image is automatically extracted, but the picture does not have a watermark function, the following small series and you see together.
Ueditor Editor does not upload images and watermark function, need to carry out two development, this example is in the PHPCMS system Baidu Editor two times to develop, add upload images and watermark function.
First open the Ueditor Editor file directory of the PHP folder, open the Uploader.class.php, the Phpcms watermark method to copy, add to this class all member methods of the last side, and then modify the following:
- Picture plus watermark
- Public function watermark ($source, $target = ", $w _pos =", $w _img = ", $w _text = ' 99danji ', $w _font = 8, $w _color = ' #f f0000 ') {
- $this->w_img = ' watermark.png ';
- $this->w_pos = 9;
- $this->w_minwidth = 400;
- $this->w_minheight = 200;
- $this->w_quality = 80;
- $this->w_pct = 85;
- $w _pos = $w _pos? $w _pos: $this->w_pos;
- $w _img = $w _img? $w _img: $this->w_img;
- if (! $this->watermark_enable | |! $this->check ($source)) return false;
- if (! $target) $target = $source;
- $w _img = Phpcms_path. $w _img;
- Define (' Www_path ', DirName (DirName (dirname (__file__)));
- $w _img = '. /.. /.. /images/water/'. $w _img;
- $source _info = getimagesize ($source);
- $source _w = $source _info[0];
- $source _h = $source _info[1];
- if ($source _w < $this->w_minwidth | | $source _h < $this->w_minheight) return false;
- Switch ($source _info[2]) {
- Case 1:
- $source _img = imagecreatefromgif ($source);
- Break
- Case 2:
- $source _img = Imagecreatefromjpeg ($source);
- Break
- Case 3:
- $source _img = imagecreatefrompng ($source);
- Break
- Default:
- return false;
- }
- if (!empty ($w _img) && file_exists ($w _img)) {
- $ifwaterimage = 1;
- $water _info = getimagesize ($w _img);
- $width = $water _info[0];
- $height = $water _info[1];
- Switch ($water _info[2]) {
- Case 1:
- $water _img = imagecreatefromgif ($w _img);
- Break
- Case 2:
- $water _img = imagecreatefromjpeg ($w _img);
- Break
- Case 3:
- $water _img = imagecreatefrompng ($w _img);
- Break
- Default:
- Return
- }
- } else {
- $ifwaterimage = 0;
- $temp = Imagettfbbox (ceil ($w _font*2.5), 0, Pc_path. ' Libs/data/font/elephant.ttf ', $w _text);
- $width = $temp [2]-$temp [6];
- $height = $temp [3]-$temp [7];
- Unset ($temp);
- }
- Switch ($w _pos) {
- Case 1:
- $WX = 5;
- $wy = 5;
- Break
- Case 2:
- $WX = ($source _w-$width)/2;
- $wy = 0;
- Break
- Case 3:
- $WX = $source _w-$width;
- $wy = 0;
- Break
- Case 4:
- $WX = 0;
- $wy = ($source _h-$height)/2;
- Break
- Case 5:
- $WX = ($source _w-$width)/2;
- $wy = ($source _h-$height)/2;
- Break
- Case 6:
- $WX = $source _w-$width;
- $wy = ($source _h-$height)/2;
- Break
- Case 7:
- $WX = 0;
- $wy = $source _h-$height;
- Break
- Case 8:
- $WX = ($source _w-$width)/2;
- $wy = $source _h-$height;
- Break
- Case 9:
- $WX = $source _w-$width;
- $wy = $source _h-$height;
- Break
- Case 10:
- $WX = rand (0, ($source _w-$width));
- $wy = rand (0, ($source _h-$height));
- Break
- Default
- $WX = rand (0, ($source _w-$width));
- $wy = rand (0, ($source _h-$height));
- Break
- }
- if ($ifwaterimage) {
- if ($water _info[2] = = 3) {
- Imagecopy ($source _img, $water _img, $wx, $wy, 0, 0, $width, $height);
- } else {
- Imagecopymerge ($source _img, $water _img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
- }
- } else {
- if (!empty ($w _color) && (strlen ($w _color) ==7)) {
- $r = Hexdec (substr ($w _color,1,2));
- $g = Hexdec (substr ($w _color,3,2));
- $b = Hexdec (substr ($w _color,5));
- } else {
- Return
- }
- Imagestring ($source _img, $w _font, $wx, $wy, $w _text,imagecolorallocate ($source _img, $r, $g, $b));
- }
- Switch ($source _info[2]) {
- Case 1:
- Imagegif ($source _img, $target);
- Break
- Case 2:
- Imagejpeg ($source _img, $target, $this->w_quality);
- Break
- Case 3:
- Imagepng ($source _img, $target);
- Break
- Default:
- Return
- }
- if (Isset ($water _info)) {
- unset ($water _info);
- }
- if (Isset ($water _img)) {
- Imagedestroy ($water _img);
- }
- unset ($source _info);
- Imagedestroy ($source _img);
- return true;
- }
- Public function Check ($image) {
- Return extension_loaded (' gd ') && preg_match ("/\. ( jpg|jpeg|gif|png)/I ", $image, $m) && file_exists ($image) && function_exists (' Imagecreatefrom '. ( $m [1] = = ' jpg '? ' JPEG ': $m [1]);
- }
Compared to the part I modified, because the PHPCMS watermark can be managed in the background settings, the PHPCMS comes with the watermark method by reading the configuration file to get the path, and read the database settings to get the parameter settings, then these places need to be set manually.
By the way, the Upfile method also adds a function:
- if ($this->watermark) {
- $this->watermark ($this->filepath, $this->filepath);
- }
Then open the Ueditor Baidu Editor php directory under the action_upload.php file, plus whether to add the watermark parameters:
- /* Upload configuration */
- $base = "Upload";
- Switch (Htmlspecialchars ($_get[' action ')) {
- Case ' Uploadimage ':
- $config = Array (
- "Pathformat" = $CONFIG [' Imagepathformat '],
- "MaxSize" = $CONFIG [' Imagemaxsize '],
- "Allowfiles" = $CONFIG [' Imageallowfiles ']
- );
- $fieldName = $CONFIG [' Imagefieldname '];
- $watermark = true;
- Break
Then there is another sentence to be changed:
- /* Generate the Upload instance object and complete the upload */
- $up = new Uploader ($fieldName, $config, $base, $watermark);
This will be done, this article is mainly to provide ideas and reference.
Form:http://www.uphtm.com/php/255.html
Add watermark for Baidu Ueditor editor upload image