Improve pull-request (move thumbnail generation function)

This commit is contained in:
Frederic Guillot 2015-04-11 13:47:31 -04:00
parent aea3a352aa
commit 3311061d10
23 changed files with 149 additions and 88 deletions

View File

@ -110,82 +110,16 @@ class File extends Base
{
$task = $this->getTask();
$file = $this->file->getById($this->request->getIntegerParam('file_id'));
$width_param = $this->request->getIntegerParam('width');
$height_param = $this->request->getIntegerParam('height');
$filename = FILES_DIR.$file['path'];
if ($file['task_id'] == $task['id'] && file_exists($filename)) {
// Get new sizes
list($width, $height) = getimagesize($filename);
//default size
if ($width_param == 0 && $height_param == 0) {
$width_param = 100;
$height_param = 100;
}
if ($width_param > 0 && $height_param == 0) {
$newwidth = $width_param;
$newheight = floor($height * ($width_param / $width));
$dest_y = 0;
$dest_x = 0;
$thumb = imagecreatetruecolor($newwidth, $newheight);
} elseif ($width_param == 0 && $height_param > 0) {
$newwidth = floor($width * ($height_param / $height));
$newheight = $height_param;
$dest_y = 0;
$dest_x = 0;
$thumb = imagecreatetruecolor($newwidth, $newheight);
} else {
// resize and cut
$ratio_img = $width / $height;
$ratio_param = $width_param / $height_param;
if($ratio_img <= $ratio_param){
$newwidth = $width_param;
$newheight = floor($height * ($width_param / $width));
$dest_y = ( $newheight - $height_param ) / 2 * (-1);
$dest_x = 0;
}elseif($ratio_img > $ratio_param){
$newwidth = floor($width * ($height_param / $height));
$newheight = $height_param;
$dest_y = 0;
$dest_x = ( $newwidth - $width_param ) / 2 * (-1);
}
$thumb = imagecreatetruecolor($width_param, $height_param);
}
// Load
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
switch ($extension) {
case 'jpeg':
case 'jpg':
$source = imagecreatefromjpeg($filename);
break;
case 'png':
$source = imagecreatefrompng($filename);
break;
case 'gif':
$source = imagecreatefromgif($filename);
break;
default:
die('File "' . $filename . '" is not valid jpg, png or gif image.');
break;
}
// Resize
imagecopyresampled($thumb, $source, $dest_x, $dest_y, 0, 0, $newwidth, $newheight, $width, $height);
$metadata = getimagesize($filename);
if (isset($metadata['mime'])) {
$this->response->contentType($metadata['mime']);
imagejpeg($thumb);
}
$this->response->contentType('image/jpeg');
$this->file->generateThumbnail(
$filename,
$this->request->getIntegerParam('width'),
$this->request->getIntegerParam('height')
);
}
}

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -849,4 +849,7 @@ return array(
'Test your device' => 'Testez votre appareil',
'Assign a color when the task is moved to a specific column' => 'Assigner une couleur lorsque la tâche est déplacée dans une colonne spécifique',
'%s via Kanboard' => '%s via Kanboard',
'uploaded by: %s' => 'Télécharger par : %s',
'uploaded on: %s' => 'Télécharger le : %s',
'size: %s' => 'Taille : %s',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
'Test your device' => 'デバイスをテストする',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
'Test your device' => 'Teste o seu dispositivo',
'Assign a color when the task is moved to a specific column' => 'Atribuir uma cor quando a tarefa é movida em uma coluna específica',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
// 'Test your device' => '',
// 'Assign a color when the task is moved to a specific column' => '',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -847,4 +847,7 @@ return array(
'Test your device' => '测试设备',
'Assign a color when the task is moved to a specific column' => '任务移动到指定栏目时设置颜色',
// '%s via Kanboard' => '',
// 'uploaded by: %s' => '',
// 'uploaded on: %s' => '',
// 'size: %s' => '',
);

View File

@ -80,6 +80,7 @@ class File extends Base
* @param string $name Filename
* @param string $path Path on the disk
* @param bool $is_image Image or not
* @param integer $size File size
* @return bool
*/
public function create($task_id, $name, $path, $is_image, $size)
@ -96,7 +97,7 @@ class File extends Base
'is_image' => $is_image ? '1' : '0',
'size' => $size,
'user_id' => $this->userSession->getId(),
'date' => time(),
'date' => time(),
));
}
@ -122,11 +123,11 @@ class File extends Base
self::TABLE.'.size',
User::TABLE.'.username',
User::TABLE.'.name as user_name'
)
)
->join(User::TABLE, 'id', 'user_id')
->eq('task_id', $task_id)
->asc(self::TABLE.'.name')
->findAll();
->asc(self::TABLE.'.name')
->findAll();
}
/**
@ -276,7 +277,7 @@ class File extends Base
$original_filename,
$destination_filename,
$this->isImage($original_filename),
$_FILES[$form_name]['size'][$key]
$_FILES[$form_name]['size'][$key]
);
}
}
@ -285,4 +286,79 @@ class File extends Base
return count(array_unique($result)) === 1;
}
/**
* Generate a jpeg thumbnail from an image (output directly the image)
*
* @access public
* @param string $filename Source image
* @param integer $resize_width Desired image width
* @param integer $resize_height Desired image height
*/
public function generateThumbnail($filename, $resize_width, $resize_height)
{
$metadata = getimagesize($filename);
$src_width = $metadata[0];
$src_height = $metadata[1];
$dst_y = 0;
$dst_x = 0;
if (empty($metadata['mime'])) {
return;
}
if ($resize_width == 0 && $resize_height == 0) {
$resize_width = 100;
$resize_height = 100;
}
if ($resize_width > 0 && $resize_height == 0) {
$dst_width = $resize_width;
$dst_height = floor($src_height * ($resize_width / $src_width));
$dst_image = imagecreatetruecolor($dst_width, $dst_height);
}
elseif ($resize_width == 0 && $resize_height > 0) {
$dst_width = floor($src_width * ($resize_height / $src_height));
$dst_height = $resize_height;
$dst_image = imagecreatetruecolor($dst_width, $dst_height);
}
else {
$src_ratio = $src_width / $src_height;
$resize_ratio = $resize_width / $resize_height;
if ($src_ratio <= $resize_ratio) {
$dst_width = $resize_width;
$dst_height = floor($src_height * ($resize_width / $src_width));
$dst_y = ($dst_height - $resize_height) / 2 * (-1);
}
else {
$dst_width = floor($src_width * ($resize_height / $src_height));
$dst_height = $resize_height;
$dst_x = ($dst_width - $resize_width) / 2 * (-1);
}
$dst_image = imagecreatetruecolor($resize_width, $resize_height);
}
switch ($metadata['mime']) {
case 'image/jpeg':
case 'image/jpg':
$src_image = imagecreatefromjpeg($filename);
break;
case 'image/png':
$src_image = imagecreatefrompng($filename);
break;
case 'image/gif':
$src_image = imagecreatefromgif($filename);
break;
default:
return;
}
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
imagejpeg($dst_image);
}
}

View File

@ -10,9 +10,9 @@ const VERSION = 62;
function version_62($pdo)
{
$pdo->exec('ALTER TABLE files ADD COLUMN date VARCHAR(10) NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN user_id INT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN size FLOAT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN `date` INT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN `user_id` INT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN `size` INT NOT NULL DEFAULT 0');
}
function version_61($pdo)

View File

@ -10,9 +10,9 @@ const VERSION = 43;
function version_43($pdo)
{
$pdo->exec('ALTER TABLE files ADD COLUMN date VARCHAR(10) NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN user_id INT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN size FLOAT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "date" INTEGER NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "user_id" INTEGER NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "size" INTEGER NOT NULL DEFAULT 0');
}
function version_42($pdo)

View File

@ -10,9 +10,9 @@ const VERSION = 61;
function version_61($pdo)
{
$pdo->exec('ALTER TABLE files ADD COLUMN date VARCHAR(10) NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN user_id INT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN size FLOAT NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "date" INTEGER NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "user_id" INTEGER NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE files ADD COLUMN "size" INTEGER NOT NULL DEFAULT 0');
}
function version_60($pdo)

View File

@ -16,7 +16,7 @@
<?php endif ?>
<p>
<?= $this->e($file['name']) ?>
<span class="column-tooltip" title='<?= $this->e(t('uploaded by').': '.$this->e($file['user_name'] ?: $file['username']).'<br>'.t('uploaded on').': '.dt('%B %e, %Y at %k:%M %p', $file['date']).'<br>'.t('size').': '.round($file['size']/1024/1024,2)).' MB' ?>'>
<span class="column-tooltip" title='<?= t('uploaded by: %s', $file['user_name'] ?: $file['username']).'<br>'.t('uploaded on: %s', dt('%B %e, %Y at %k:%M %p', $file['date'])).'<br>'.t('size: %s', $this->formatBytes($file['size'])) ?>'>
<i class="fa fa-info-circle"></i>
</span>
</p>
@ -38,7 +38,7 @@
<td><i class="fa <?= $this->getFileIcon($file['name']) ?> fa-fw"></i></td>
<td>
<?= $this->e($file['name']) ?>
<span class="column-tooltip" title='<?= $this->e(t('uploaded by').': '.$this->e($file['user_name'] ?: $file['username']).'<br>'.t('uploaded on').': '.dt('%B %e, %Y at %k:%M %p', $file['date']).'<br>'.t('size').': '.round($file['size']/1024/1024,2)).' MB' ?>'>
<span class="column-tooltip" title='<?= t('uploaded by: %s', $file['user_name'] ?: $file['username']).'<br>'.t('uploaded on: %s', dt('%B %e, %Y at %k:%M %p', $file['date'])).'<br>'.t('size: %s', $this->formatBytes($file['size'])) ?>'>
<i class="fa fa-info-circle"></i>
</span>
</td>