Checking the width of an image file

There was a plugin in WordPress that I used which allows an image to be uploaded.  This part of the code was done in Javascript and not in PHP.  When the image file is uploaded, I want it to prompt a message if the width of the image is smaller than 700px.

In Javascript, once the file is uploaded, it is put into a file object.  As with any other object, we could get other properties of this file object such as its name.  If the file object is file, we could get the name by using

Making use of file object property, this is how I get the full path of the image file and pass it to img.src to check its width.

var d = new Date();
var mth = d.getMonth() + 1;
var str1;
if (mth == 11) {
    str1 = '//localhost/wordpress/wp-content/uploads/2015/11/';
else if (mth == 12) {
    str1 = '//localhost/wordpress/wp-content/uploads/2015/12/';
var str2 = str1.concat(;

var img = new Image();
img.onload = function () {
    if (this.width < 700) {
        alert('The size of the photo is too small.');
img.src = str2;

Leave a comment

Leave a Reply