[英]How can i get image dimensions after upload with jQuery File Upload?
當我嘗試通過jQuery File Upload上傳文件時,我的回答如下。
是否可以在此結果中添加圖像尺寸,例如width: 300px
和height: 400px
? 我嘗試根據需要編輯UploadHandler.php
,但無法成功。 set_additional_file_properties
中有一個名為set_additional_file_properties
的UploadHandler.php
。 我試圖向該函數添加一個自定義變量,但是它失敗了,因為我認為在文件上傳過程之前調用了該函數。 它甚至找不到要獲取尺寸的文件。 我不知道為什么我可能會尋找文檔的錯誤部分。
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;
}
}
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;
}
...
}
如果遇到您的問題(您想在服務器端的圖像上添加寬度和高度信息),則應該會有所幫助:
http://php.net/manual/function.getimagesize.php
返回最多包含7個元素的數組。 並非所有圖像類型都將包含通道和位元素。 索引0和1分別包含圖像的寬度和高度。
=> getimagesize($ file)
只需更改UploadHandler.php
內容的set_additional_file_properties($file)
函數中的set_additional_file_properties($file)
:
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;
}
}
剛剛添加了以下幾行:
$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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.