ResizeImageController


SUBMITTED BY: Guest

DATE: Jan. 22, 2014, 12:19 p.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 4882

  1. public with sharing class ResizeImageController
  2. {
  3. public Attachment theAttachment{get;set;}
  4. public Attachment thumbnail{get;set;}
  5. public String resizedBase64Value{get;set;}
  6. public ResizeImageController()
  7. {
  8. thumbnail = new Attachment();
  9. theAttachment = new Attachment();
  10. }
  11. public String theBase64Value
  12. {
  13. get
  14. {
  15. if(theAttachment.Body != null)
  16. {
  17. theBase64Value = EncodingUtil.base64Encode(theAttachment.Body);
  18. theAttachment.Body = null;//null out as we don't need this any more.helps to reduce view state
  19. }
  20. return theBase64Value;
  21. }
  22. set;
  23. }
  24. public void doAction()
  25. {
  26. //doesn't do any thing but the uploaded image is put in the view state
  27. }
  28. //code to save the resized image
  29. public void saveThumbNail()
  30. {
  31. //create any object to save the attachment to
  32. Merchandise__c theMerchandise = new Merchandise__c(); //custom object to attach the attachment to
  33. theMerchandise.Name = 'test merchandise'+DateTime.now();
  34. theMerchandise.Description__c = 'test merchandise';
  35. theMerchandise.Price__c = 55;
  36. theMerchandise.Total_Inventory__c = 5;
  37. insert theMerchandise;
  38. thumbnail.ParentId = theMerchandise.Id;
  39. thumbnail.Name = theAttachment.Name;
  40. String tempvalue = resizedBase64Value.substringAfterLast('base64,');//apex doesn't seem to like this MIME type info .so,removing that part.
  41. thumbnail.Body = EncodingUtil.base64Decode(tempvalue);
  42. insert thumbnail;
  43. }
  44. }

comments powered by Disqus