fozuse
fozuse

Reputation: 784

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;
    }
    ...
}

Upvotes: 1

Views: 529

Answers (2)

ptCoder
ptCoder

Reputation: 2247

Only need to change UploadHandler.php file in set_additional_file_properties($file) function for the following:

protected function set_additional_file_properties($file) {
    $filesize = getimagesize("files/".$file->name); //'files' folder is default folder to upload
    $width = $filesize[0];
    $height = $filesize[1];
    if ($filesize) {
        $file->width = $width;
        $file->height = $height;
    }
    $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;
    }
}

just added these lines:

$filesize = getimagesize("files/".$file->name); //'files' folder is default folder to upload
$width = $filesize[0];
$height = $filesize[1];
if ($filesize) {
    $file->width = $width;
    $file->height = $height;
}

Upvotes: 0

Schnodderbalken
Schnodderbalken

Reputation: 3467

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)

Upvotes: 1

Related Questions