Search code examples
javascriptphpjqueryjquery-file-upload

How can i get image dimensions after upload with jQuery File Upload?


When i try to upload a file via jQuery File Upload i am taking below response.

It returns "false"

Is it posible to add image dimensions to this result like width: 300px and height: 400px? I try to edit UploadHandler.php for that need but i can't success. There is a function called set_additional_file_properties in UploadHandler.php. I tried to add a custom variable to the function but it fails because i think this function called before the file upload process. It even could not find the file to get dimensions. I don't know why probably i am looking to wrong side of the document.

protected function set_additional_file_properties($file) {
    $file->dimensions = file_exists($file->url);
    $file->deleteUrl = $this->options['script_url']
        .$this->get_query_separator($this->options['script_url'])
        .$this->get_singular_param_name()
        .'='.rawurlencode($file->name);
    $file->deleteType = $this->options['delete_type'];
    if ($file->deleteType !== 'DELETE') {
        $file->deleteUrl .= '&_method=DELETE';
    }
    if ($this->options['access_control_allow_credentials']) {
        $file->deleteWithCredentials = true;
    }
}

Solution

protected function set_additional_file_properties($file) {
    $a = getimagesize(realpath(dirname($file->url))."/".$file->name);
    $width = $a[0];
    $height = $a[1];
    if ($a) {
        $file->width = $width;
        $file->height = $height;
    }
    ...
}

Solution

  • If I get your problem (you want to add width and height information to the image on the server side) then this should help:

    http://php.net/manual/function.getimagesize.php

    Returns an array with up to 7 elements. Not all image types will include the channels and bits elements. Index 0 and 1 contains respectively the width and the height of the image.

    => getimagesize($file)