Baidu UE Editor Upload image add watermark function _php Example

Source: Internet
Author: User
Tags rand

Ueditor Editor upload image is automatically extracted, but the image does not have a watermark function, the following small series and you see together.

Ueditor Editor does not upload pictures and watermark function, need to carry out two development, this example is in the PHPCMS system to Baidu editor for two times to develop, add upload image and watermark function.

First open the Ueditor Editor file directory of the PHP folder, open the Uploader.class.php, the Phpcms add Watermark method to copy, add to the end of all members of this class method, and then modify the following:

Picture watermark Public Function watermark ($source, $target = ', $w _pos = ', $w _img = ', $w _text = ' 99danji ', $w _font = 8, $w _co
  lor = ' #ff0000 ') {$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: $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, $he
    ight);
     } 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; The 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 in the background management settings, Phpcms watermark method by reading the configuration file to get the path, and read the database settings get parameter settings, then these places need to manually set.

By the way, add a function to the Upfile method:

Copy Code code as follows:

if ($this->watermark) {
$this->watermark ($this->filepath, $this->filepath);
}

Then open ueditor Baidu Editor php directory under the action_upload.php file, plus whether to add 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

And then there's another sentence to be changed:

Copy Code code as follows:

/* Generate upload instance object and complete upload * *
$up = new Uploader ($fieldName, $config, $base, $watermark);

This is done, this article is mainly to provide ideas and references.

The above mentioned is the entire content of this article, I hope you can enjoy, you can use the UE editor to help.

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.