Add a watermark to the image of the product listing

/ Add watermark to product details
 public  abstract  class WaterMarkUtil {

  // Commodity details are generally rich text or html content, and there will be many pictures in it.
   // Adding a watermark to a picture is to spell the watermark address on the url of the picture, for example http:xxx.a.jpg and add the watermark to http:xxx .a.jpg?x-oss-process=img/xxxxxx 
  public  static   String addWaterMark(String content,String markUrl){
    StringBuilder sb = new StringBuilder();
    if(StringUtils.isBlank(content) || StringUtils.isBlank(markUrl)) {
        return content;
    }
    Document document = Jsoup.parse(content); // parse into xml or html object 
    if ( null == document){
         return content;
    }
    // Get all img elements 
    Elements imgs = document.select("img" );
     if ( null ==imgs || imgs.size()==0 ){
         return content;
    }
    // Replace with watermark image 
    for (Element img : imgs) {
        String picUrl = img.attr("src");
        if(StringUtils.isBlank(picUrl)){
            continue;
        }
        sb.append(picUrl).append("?").append(markUrl);
        img.attr("src",sb.toString());
        sb.delete(0,sb.length());
    }
      return document.toString();
  }


}

 

Reprinted in: https://www.cnblogs.com/yangxiaohui227/p/11064400.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324124850&siteId=291194637