I did upload image into record in this way but i need how to add watermark on after upload image.If know any idea please say to me.

Apex class:

public class UploadImage
   public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '00190000010E7V9'; // the record the file is attached to
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;

Visualforce page:

<apex:page controller="UploadImage">

<style type="text/css">
div.resized_image p {
  margin: 2px;
  margin-top: 0;
  font-size: 8px;
  background: url(http://i242.photobucket.com/albums/ff244/9861_omikron/error.png) no-repeat;
  padding-left: 20px;
  color: #333;


<script type="text/javascript">
   var loadFile = function(event) {
    var output = document.getElementById('output');
    output.src = URL.createObjectURL(event.target.files[0]);
    loadfile.resize(50, 50);
$(document).ready(function() {
    function(maxht, maxwt, minht, minwt) {
      var imgs = document.getElementsByTagName('img');
      var resize_image = function(img, newht, newwt) {
        img.height = newht;
        img.width  = newwt;
        $(img).wrap('<table><tr><td class="tborder"><div class="resized_image"><a href="' + img.src + '" target="_blank"></a></div></td></tr></table>');
        $(img).parent().before('<p>NOTE: This image was resized. To view it full-size, click on the image.</p>');
        $(img).parent().after('<p style="text-align:right;background:none;margin:0;padding-right:3px">Image resizing script by <a href="http://aetus.net/217/programming/automatically-resize-large-images-with-javascript/">Aetus Designs</a>.</p>');

      for (var i = 0; i < imgs.length; i++) {
        var img = imgs[i];
        if (img.height > maxht || img.width > maxwt) {
          var old_ratio = img.height / img.width;
          var min_ratio = minht / minwt;
          if (old_ratio === min_ratio) {
            resize_image(img, minht, minwt);
          else {
            var newdim = [img.height, img.width];
            newdim[0] = minht;
            newdim[1] = newdim[0] / old_ratio;
            if (newdim[1] > maxwt) {
              newdim[1] = minwt;
              newdim[0] = newdim[1] * old_ratio;
            resize_image(img, newdim[0], newdim[1]);
  )(500, 500, 500, 500);
  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a Attachment">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="resize_image" accept="image/*" onchange="loadFile(event)"/>
           <img id="output"/><br/>


1 Answer 1



As commented on your previous question on this How to watermark a Image (gif,jpeg and bmp) in visualforce?, the Apex language does not include the necessary libraries or indeed the ability to directly manipulate bytes that would be needed to write those libraries to do this sort of work. (The 10 second CPU limit might also be a block.)

So you have no choice but to make a web service callout to an external system - pass the image over and receive the image back - to get this work done. If you are very lucky such a service might already exist - Google to see - but if not you will have to write it yourself and host it yourself. I would start by trying to find the best watermarking libraries and then whatever language they use would help make the hosting decision e.g. Heroku supports a number of languages.


You don't explain why you want to add the watermark, but if you don't mind the small risk of expert users tampering with the process, you could use JavaScript to do the watermarking; watermark.js is one library you could use for that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .