Image.save (rawimage, dir, maxWidth, maxHeight)
save image as file on local disk but before check if image should be resized

  • Parameter Object:
    uploaded image
  • Parameter Object:
    File-Object representing the destination directory
  • Parameter Int:
    maximum width
  • Parameter Int:
    maximum height
Sourcecode in Image/objectFunctions.js:
1:   function save(rawimage, dir, maxWidth, maxHeight) {
2:      // determine filetype of image (one could do this also by checking the mimetype)
3:      this.fileext = evalImgType(rawimage.contentType);
4:      if (this.fileext == "ico") {
5:         // the image is an .ico, so we directory write it to disk and return
6:         rawimage.writeToFile(dir.getPath(), this.filename + "." + this.fileext);
7:         return true;
8:      }
9:      var img = new Helma.Image(rawimage.getContent());
10:     this.width = img.getWidth();
11:     this.height = img.getHeight();
12:     var resize = false;
13:     var hfact = 1;
14:     var vfact = 1;
15:     if (maxWidth && this.width > maxWidth) {
16:        hfact = maxWidth / this.width;
17:        resize = true;
18:     }
19:     if (maxHeight && this.height > maxHeight) {
20:        vfact = maxHeight / this.height;
21:        resize = true;
22:     }
23:  
24:     if (resize) {
25:        this.width = Math.ceil(this.width * (hfact < vfact ? hfact : vfact));
26:        this.height = Math.ceil(this.height * (hfact < vfact ? hfact : vfact));
27:        try {
28:           img.resize(this.width, this.height);
29:           if (rawimage.contentType == 'image/gif' || this.fileext == "gif")
30:              img.reduceColors(256);
31:        } catch (err) {
32:           throw new Exception("imageResize");
33:        }
34:     }
35:     // finally we try  to save the resized image
36:     try {
37:        if (resize)
38:           img.saveAs(dir.getPath() + "/" + this.filename + "." + this.fileext);
39:        else
40:           rawimage.writeToFile(dir.getPath(), this.filename + "." + this.fileext);
41:     } catch (err) {
42:        app.log("Error in image.save(): can't save image to "+dir);
43:        throw new Exception("imageSave");
44:     }
45:     var f = new Helma.File(dir.getPath(), this.filename + "." + this.fileext);
46:     this.filesize = f.getLength();
47:     return;
48:  }