Skip to content

Commit

Permalink
feature 3067: upload any file type with the new HTML5 upload form.
Browse files Browse the repository at this point in the history
$file_types = conf['upload_form_all_types'] ? $conf['file_ext'] : $conf['picture_ext'];

By default, conf['upload_form_all_types'] = false;


git-svn-id: http://piwigo.org/svn/trunk@29124 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
plegall committed Jul 30, 2014
1 parent cd110d9 commit 379398d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 23 deletions.
89 changes: 87 additions & 2 deletions admin/include/functions_upload.inc.php
Expand Up @@ -219,6 +219,7 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
$file_path = $upload_dir.'/'.$filename_wo_ext.'.';

list($width, $height, $type) = getimagesize($source_filepath);

if (IMAGETYPE_PNG == $type)
{
$file_path.= 'png';
Expand All @@ -232,10 +233,27 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
$is_tiff = true;
$file_path.= 'tif';
}
else
elseif (IMAGETYPE_JPEG == $type)
{
$file_path.= 'jpg';
}
elseif (isset($conf['upload_form_all_types']) and $conf['upload_form_all_types'])
{
$original_extension = strtolower(get_extension($original_filename));

if (in_array($original_extension, $conf['file_ext']))
{
$file_path.= $original_extension;
}
else
{
die('unexpected file type');
}
}
else
{
die('forbidden file type');
}

prepare_directory($upload_dir);
}
Expand Down Expand Up @@ -294,6 +312,62 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
}
}

//
// generate pwg_representative in case of video
//
$ffmpeg_video_exts = array( // extensions tested with FFmpeg
'wmv','mov','mkv','mp4','mpg','flv','asf','xvid','divx','mpeg',
'avi','rm',
);

if (isset($original_extension) and in_array($original_extension, $ffmpeg_video_exts))
{
$representative_file_path = dirname($file_path).'/pwg_representative/';
$representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';

$representative_ext = 'jpg';
$representative_file_path.= $representative_ext;

prepare_directory(dirname($representative_file_path));

$second = 1;

$ffmpeg = $conf['ffmpeg_dir'].'ffmpeg';
$ffmpeg.= ' -i "'.$file_path.'"';
$ffmpeg.= ' -an -ss '.$second;
$ffmpeg.= ' -t 1 -r 1 -y -vcodec mjpeg -f mjpeg';
$ffmpeg.= ' "'.$representative_file_path.'"';

// file_put_contents('/tmp/ffmpeg.log', "\n==== ".date('c')."\n".__FUNCTION__.' : '.$ffmpeg."\n", FILE_APPEND);

@exec($ffmpeg);

if (!file_exists($representative_file_path))
{
$representative_ext = null;
}
}

if (isset($original_extension) and 'pdf' == $original_extension and pwg_image::get_library() == 'ext_imagick')
{
$representative_file_path = dirname($file_path).'/pwg_representative/';
$representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';

$representative_ext = 'jpg';
$representative_file_path.= $representative_ext;

prepare_directory(dirname($representative_file_path));

$exec = $conf['ext_imagick_dir'].'convert';
$exec.= ' -quality 98';
$exec.= ' "'.realpath($file_path).'"[0]';

$dest = pathinfo($representative_file_path);
$exec.= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'"';
$exec.= ' 2>&1';
@exec($exec, $returnarray);
}

if (pwg_image::get_library() != 'gd')
{
if ($conf['original_resize'])
Expand Down Expand Up @@ -476,7 +550,18 @@ function pwg_image_infos($path)

function is_valid_image_extension($extension)
{
return in_array(strtolower($extension), array('jpg', 'jpeg', 'png', 'gif'));
global $conf;

if (isset($conf['upload_form_all_types']) and $conf['upload_form_all_types'])
{
$extensions = $conf['file_ext'];
}
else
{
$extensions = $conf['picture_ext'];
}

return array_unique(array_map('strtolower', $extensions));
}

function file_upload_error_message($error_code)
Expand Down
20 changes: 8 additions & 12 deletions admin/include/photos_add_direct_prepare.inc.php
Expand Up @@ -93,21 +93,17 @@
)
);

$upload_file_types = 'jpeg, png, gif';

if (pwg_image::get_library() == 'ext_imagick')
{
$upload_file_types.= ', tiff';
$template->assign('tif_enabled', true);
}
$unique_exts = array_unique(
array_map(
'strtolower',
$conf['upload_form_all_types'] ? $conf['file_ext'] : $conf['picture_ext']
)
);

if (false) // TODO manage zip files in pwg.images.upload
{
$upload_file_types.= ', zip';
}
$template->assign(
array(
'upload_file_types' => $upload_file_types,
'upload_file_types' => implode(', ', $unique_exts),
'file_exts' => implode(',', $unique_exts),
)
);

Expand Down
3 changes: 1 addition & 2 deletions admin/themes/default/template/photos_add_direct.tpl
Expand Up @@ -80,8 +80,7 @@ jQuery(document).ready(function(){
max_file_size : '1000mb',
// Specify what files to browse for
mime_types: [
{title : "Image files", extensions : "jpeg,jpg,gif,png"},
{title : "Zip files", extensions : "zip"}
{title : "Image files", extensions : "{/literal}{$file_exts}{literal}"}
]
},

Expand Down
24 changes: 17 additions & 7 deletions include/config_default.inc.php
Expand Up @@ -53,15 +53,15 @@
//
// $conf['order_by_inside_category_custom'] = $conf['order_by_custom'];

// file_ext : file extensions (case sensitive) authorized
$conf['file_ext'] = array('jpg','JPG','jpeg','JPEG',
'png','PNG','gif','GIF','mpg','zip',
'avi','mp3','ogg');

// picture_ext : file extensions for picture file, must be a subset of
// file_ext
$conf['picture_ext'] = array('jpg','JPG','jpeg','JPEG',
'png','PNG','gif','GIF');
$conf['picture_ext'] = array('jpg','JPG','jpeg','JPEG','png','PNG','gif','GIF');

// file_ext : file extensions (case sensitive) authorized
$conf['file_ext'] = array_merge(
$conf['picture_ext'],
array('tiff', 'tif', 'mpg','zip','avi','mp3','ogg')
);

// top_number : number of element to display for "best rated" and "most
// visited" categories
Expand Down Expand Up @@ -798,4 +798,14 @@
// 'png' or 'jpg': your uploaded TIF photos will have a representative in
// JPEG or PNG file format
$conf['tiff_representative_ext'] = 'png';

// in the upload form, let users upload only picture_exts or all file_exts?
// for some file types, Piwigo will try to generate a pwg_representative
// (TIFF, videos, PDF)
$conf['upload_form_all_types'] = false;

// If we try to generate a pwg_representative for a video we use ffmpeg. If
// "ffmpeg" is not visible by the web user, you can define the full path of
// the directory where "ffmpeg" executable is.
$conf['ffmpeg_dir'] = '';
?>
1 change: 1 addition & 0 deletions include/ws_functions/pwg.images.php
Expand Up @@ -1359,6 +1359,7 @@ function ws_images_upload($params, $service)
SELECT
id,
name,
representative_ext,
path
FROM '.IMAGES_TABLE.'
WHERE id = '.$image_id.'
Expand Down

0 comments on commit 379398d

Please sign in to comment.