* finish task #2465.

This commit is contained in:
wangyidong
2016-02-17 16:22:24 +08:00
parent d01b8eb8df
commit 8230ffd6d0
3 changed files with 120 additions and 2 deletions

View File

@@ -5,3 +5,4 @@ $config->file->mimes['csv'] = 'text/csv';
$config->file->mimes['default'] = 'application/octet-stream';
$config->file->imageExtensions = array('jpeg', 'jpg', 'gif', 'png');
$config->file->imageFmt = array('.jpg', '.bmp', '.jpeg');

View File

@@ -51,13 +51,21 @@ class file extends control
if($file['size'] == 0) die(json_encode(array('error' => 1, 'message' => $this->lang->file->errorFileUpload)));
if(@move_uploaded_file($file['tmpname'], $this->file->savePath . $file['pathname']))
{
$url = $this->file->webPath . $file['pathname'];
/* Compress image for jpg and bmp. */
$compressedImage = $this->file->compressImage($file['pathname']);
if($compressedImage)
{
$file['pathname'] = $compressedImage['pathname'];
$file['extension'] = $compressedImage['extension'];
$file['size'] = $compressedImage['size'];
}
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::today();
unset($file['tmpname']);
$this->dao->insert(TABLE_FILE)->data($file)->exec();
$url = $this->file->webPath . $file['pathname'];
die(json_encode(array('error' => 0, 'url' => $url)));
}
else

View File

@@ -78,6 +78,14 @@ class fileModel extends model
{
if($file['size'] == 0) continue;
move_uploaded_file($file['tmpname'], $this->savePath . $file['pathname']);
$compressedImage = $this->compressImage($file['pathname']);
if($compressedImage)
{
$file['pathname'] = $compressedImage['pathname'];
$file['extension'] = $compressedImage['extension'];
$file['size'] = $compressedImage['size'];
}
$file['objectType'] = $objectType;
$file['objectID'] = $objectID;
$file['addedBy'] = $this->app->user->account;
@@ -238,10 +246,11 @@ class fileModel extends model
$realPathName= $this->savePath . $pathName;
if(!is_dir(dirname($realPathName)))mkdir(dirname($realPathName));
move_uploaded_file($file['tmpname'], $realPathName);
$this->compressImage($pathName);
$fileInfo->addedBy = $this->app->user->account;
$fileInfo->addedDate = helper::now();
$fileInfo->size = $file['size'];
$fileInfo->size = filesize($realPathName);
$this->dao->update(TABLE_FILE)->data($fileInfo)->where('id')->eq($fileID)->exec();
return true;
}
@@ -466,4 +475,104 @@ class fileModel extends model
}
return $data;
}
/**
* Compress image
*
* @param string $pathName
* @access public
* @return array
*/
public function compressImage($pathName)
{
if(!extension_loaded('gd')) return false;
$fileName = $this->savePath . $pathName;
$suffix = strrchr($fileName, '.');
$lowerSuffix = strtolower($suffix);
if(!in_array($lowerSuffix, $this->config->file->imageFmt)) return false;
$quality = 85;
$newSuffix = '.jpg';
$compressedName = str_replace($suffix, $newSuffix, $pathName);
$res = '';
if($lowerSuffix == '.bmp')
{
$res = $this->imagecreatefrombmp($fileName);
}
else
{
$res = imagecreatefromjpeg($fileName);
}
imagejpeg($res, $this->savePath . $compressedName, $quality);
if($fileName != $this->savePath . $compressedName) unlink($fileName);
return array('pathname' => $compressedName, 'extension' => ltrim($newSuffix, '.'), 'size' => filesize($this->savePath . $compressedName));
}
/**
* Read 24bit BMP files
* Author: de77
* Licence: MIT
* Webpage: de77.com
* Version: 07.02.2010
* Source : https://github.com/acustodioo/pic/blob/master/imagecreatefrombmp.function.php
*
* @param string $filename
* @access public
* @return resource
*/
public function imagecreatefrombmp($filename) {
$f = fopen($filename, "rb");
//read header
$header = fread($f, 54);
$header = unpack('c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/'.
'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'.
'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors', $header);
if ($header['identifier1'] != 66 or $header['identifier2'] != 77)
return false;
if ($header['bits_per_pixel'] != 24)
return false;
$wid2 = ceil((3 * $header['width']) / 4) * 4;
$wid = $header['width'];
$hei = $header['height'];
$img = imagecreatetruecolor($header['width'], $header['height']);
//read pixels
for ($y = $hei - 1; $y >= 0; $y--) {
$row = fread($f, $wid2);
$pixels = str_split($row, 3);
for ($x = 0; $x < $wid; $x++) {
imagesetpixel($img, $x, $y, $this->dwordize($pixels[$x]));
}
}
fclose($f);
return $img;
}
/**
* Dwordize for imagecreatefrombmp
*
* @param streing $str
* @access private
* @return int
*/
private function dwordize($str)
{
$a = ord($str[0]);
$b = ord($str[1]);
$c = ord($str[2]);
return $c * 256 * 256 + $b * 256 + $a;
}
}