image-upload now checks mimetype instead of extension

This commit is contained in:
Robert Gaggl 2001-06-28 17:58:32 +00:00
parent a28fc411f3
commit a1e026573b

View file

@ -1,35 +1,32 @@
/**
* save image as file on local disk
* but before check if image should be resized
* input params: - uploaded image
* - parameter-object
* return param: - parameter-object with add. props
*/
function saveImg(rawimage) {
if (rawimage && (!rawimage.contentType || rawimage.contentType.indexOf("image/") < 0)) {
if (rawimage && (!rawimage.contentType || !this.evalImgType(rawimage.contentType))) {
// whatever the user has uploaded, it was no image!
this.cache.error = true;
res.message = "This was definetly no image!";
} else {
// determine filetype of image (one could do this also by checking the mimetype)
this.fileext = rawimage.name.substring(rawimage.name.lastIndexOf(".") + 1);
this.fileext = this.evalImgType(rawimage.contentType);
var img = new Image(rawimage.getContent());
// check if resizing is necessary
if (this.cache.maxWidth && this.cache.maxHeight && img.width > this.cache.maxWidth && img.height > this.cache.maxHeight) {
var hfact = this.cache.maxWidth / img.width;
var vfact = this.cache.maxHeight / img.height;
if (this.cache.maxwidth && this.cache.maxheight && img.width > this.cache.maxwidth && img.height > this.cache.maxheight) {
var hfact = this.cache.maxwidth / img.width;
var vfact = this.cache.maxheight / img.height;
this.width = Math.round(img.width * (hfact < vfact ? hfact : vfact));
this.height = Math.round(img.height * (hfact < vfact ? hfact : vfact));
var doResize = true;
} else if (this.cache.maxWidth && img.width > this.cache.maxWidth) {
var fact = this.cache.maxWidth / img.width;
this.width = this.cache.maxWidth;
} else if (this.cache.maxwidth && img.width > this.cache.maxwidth) {
var fact = this.cache.maxwidth / img.width;
this.width = this.cache.maxwidth;
this.height = Math.round(img.height * fact);
var doResize = true;
} else if (this.cache.maxHeight && img.height > this.cache.maxHeight) {
var fact = this.cache.maxHeight / img.height;
this.height = this.cache.maxHeight;
} else if (this.cache.maxheight && img.height > this.cache.maxheight) {
var fact = this.cache.maxheight / img.height;
this.height = this.cache.maxheight;
this.width = Math.round(img.width * fact);
var doResize = true;
} else {
@ -63,4 +60,21 @@ function evalImg() {
res.message = "Changes saved successfully!";
res.redirect(this.weblog.images.href());
}
}
/**
* function returns file-extension according to mimetype of raw-image
* returns false if mimetype is unknown
*/
function evalImgType(ct) {
if (ct == "image/jpeg" || ct == "image/pjpeg")
return ("jpg");
else if (ct == "image/gif")
return ("gif");
else if (ct == "image/png")
return ("png");
else
return false;
}