06-image-resize.agi 931 B

123456789101112131415161718192021222324252627
  1. /*---
  2. title: Resize and crop images
  3. group: Media
  4. summary: imagelib: dimensions, a thumbnail, a crop and an inline preview.
  5. ---*/
  6. requirelib("filelib");
  7. requirelib("imagelib");
  8. var src = "user:/Photo/sunset.jpg";
  9. var size = imagelib.getImageDimension(src);
  10. console.log("Original: " + size[0] + " x " + size[1]);
  11. if (!filelib.fileExists("user:/Photo/thumbs")) {
  12. filelib.mkdir("user:/Photo/thumbs");
  13. }
  14. // A height of 0 keeps the aspect ratio.
  15. imagelib.resizeImage(src, "user:/Photo/thumbs/sunset_200.jpg", 200, 0);
  16. imagelib.cropImage(src, "user:/Photo/thumbs/sunset_crop.png", 160, 100, 320, 200);
  17. var thumb = imagelib.getImageDimension("user:/Photo/thumbs/sunset_200.jpg");
  18. console.log("Thumbnail: " + thumb[0] + " x " + thumb[1]);
  19. // renderPreview with an empty destination returns a data URL, which the
  20. // Response tab shows as an image.
  21. var preview = imagelib.renderPreview(src, "", 320, 200);
  22. sendResp(preview.dataURL);