diff options
author | plegall <plg@piwigo.org> | 2005-01-16 11:13:30 +0000 |
---|---|---|
committer | plegall <plg@piwigo.org> | 2005-01-16 11:13:30 +0000 |
commit | d5c2a2eda88f89bd3ed40523283fd601e90216ab (patch) | |
tree | 14ae7f3aded18e19ae294a7e1c12d8dcac01b3a6 /admin | |
parent | cc5ba30bd061d00be17bb26061962016e4112b9d (diff) |
- big update in thumbnail creation process : form is always shown with the
comprehensive list of pictures without thumbnails (no matter the
directory) unless there are no thumbnail missing.
- thumbnail creation process : form parameters are saved between 2 thumbnail
generations thanks to new process
- thumbnail creation process : can ask to create all missing thumbnails
- thumbnail creation process : default thumbnail width and height are
configured in include/config.inc.php
- thumbnail creation process : errors due to no write access directories are
catched
git-svn-id: http://piwigo.org/svn/trunk@695 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'admin')
-rw-r--r-- | admin/thumbnail.php | 487 |
1 files changed, 221 insertions, 266 deletions
diff --git a/admin/thumbnail.php b/admin/thumbnail.php index a300c26ca..60982caa6 100644 --- a/admin/thumbnail.php +++ b/admin/thumbnail.php @@ -26,104 +26,30 @@ // +-----------------------------------------------------------------------+ include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' ); //------------------------------------------------------------------- functions -// get_subdirs returns an array containing all sub directory names, -// excepting : '.', '..' and 'thumbnail'. -function get_subdirs( $dir ) +// RatioResizeImg creates a new picture (a thumbnail since it is supposed to +// be smaller than original picture !) in the sub directory named +// "thumbnail". +function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext) { - $sub_dirs = array(); - if ( $opendir = opendir( $dir ) ) - { - while ( $file = readdir( $opendir ) ) - { - if ($file != 'thumbnail' - and $file != 'pwg_representative' - and $file != 'pwg_high' - and $file != '.' - and $file != '..' - and is_dir($dir.'/'.$file)) - { - array_push( $sub_dirs, $file ); - } - } - } - return $sub_dirs; -} + global $conf, $lang, $errors; -// get_images_without_thumbnail returns an array with all the picture names -// that don't have associated thumbnail in the directory. Each picture name -// is associated with the width, heigh and filesize of the picture. -function get_images_without_thumbnail( $dir ) -{ - $images = array(); - if ( $opendir = opendir( $dir ) ) + $filename = basename($path); + $dirname = dirname($path); + + // extension of the picture filename + $extension = get_extension($filename); + + if ($extension == 'jpg' or $extension == 'JPG') { - while ( $file = readdir( $opendir ) ) - { - $path = $dir.'/'.$file; - if ( is_image( $path, true ) ) - { - if ( !TN_exists( $dir, $file ) ) - { - $image_infos = getimagesize( $path ); - $size = floor( filesize( $path ) / 1024 ). ' KB'; - array_push( $images, array( 'name' => $file, - 'width' => $image_infos[0], - 'height' => $image_infos[1], - 'size' => $size ) ); - } - } - } + $srcImage = @imagecreatefromjpeg($path); } - return $images; -} - -// phpwg_scandir scans a dir to find pictures without thumbnails. Once found, -// creation of the thumbnails (RatioResizeImg). Only the first $_POST['n'] -// pictures without thumbnails are treated. -// scandir returns an array with the generation time of each thumbnail (for -// statistics purpose) -function phpwg_scandir( $dir, $width, $height ) -{ - global $conf; - $stats = array(); - if ( $opendir = opendir( $dir ) ) + else if ($extension == 'png' or $extension == 'PNG') { - while ( $file = readdir ( $opendir ) ) - { - $path = $dir.'/'.$file; - if ( is_image( $path, true ) ) - { - if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) ) - { - $starttime = get_moment(); - $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' ); - $endtime = get_moment(); - $info['time'] = ( $endtime - $starttime ) * 1000; - array_push( $stats, $info ); - } - } - } + $srcImage = @imagecreatefrompng($path); } - return $stats; -} - -// RatioResizeImg creates a new picture (a thumbnail since it is supposed to -// be smaller than original picture !) in the sub directory named -// "thumbnail". -function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext ) -{ - global $conf, $lang; - // full path to picture - $filepath = $path.$filename; - // extension of the picture filename - $extension = get_extension( $filepath ); - switch( $extension ) + else { - case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; - case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; - case 'png': $srcImage = @imagecreatefrompng( $filepath ); break; - case 'PNG': $srcImage = @imagecreatefrompng( $filepath ); break; - default : unset( $extension ); break; + unset($extension); } if ( isset( $srcImage ) ) @@ -168,31 +94,38 @@ function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext ) imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, $destWidth,$destHeight,$srcWidth,$srcHeight ); } - - - if( !is_dir( $path.'thumbnail' ) ) + + $tndir = $dirname.'/thumbnail'; + if (!is_dir($tndir)) { - umask( 0000 ); - mkdir( $path.'thumbnail', 0777 ); + if (!is_writable($dirname)) + { + array_push($errors, '['.$dirname.'] : '.$lang['no_write_access']); + return false; + } + umask(0000); + mkdir($tndir, 0777); } - $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail']; - $dest_file.= get_filename_wo_extension( $filename ); + + $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; + $dest_file.= get_filename_wo_extension($filename); $dest_file.= '.'.$tn_ext; - + // creation and backup of final picture - imagejpeg( $destImage, $dest_file ); + if (!is_writable($tndir)) + { + array_push($errors, '['.$tndir.'] : '.$lang['no_write_access']); + return false; + } + imagejpeg($destImage, $dest_file); // freeing memory ressources imagedestroy( $srcImage ); imagedestroy( $destImage ); - - list( $width,$height ) = getimagesize( $filepath ); - $size = floor( filesize( $filepath ) / 1024 ).' KB'; - list( $tn_width,$tn_height ) = getimagesize( $dest_file ); - $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB'; - $info = array( 'file' => $filename, - 'width' => $width, - 'height' => $height, - 'size' => $size, + + list($tn_width, $tn_height) = getimagesize($dest_file); + $tn_size = floor(filesize($dest_file) / 1024).' KB'; + + $info = array( 'path' => $path, 'tn_file' => $dest_file, 'tn_width' => $tn_width, 'tn_height' => $tn_height, @@ -218,36 +151,17 @@ function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext ) $errors = array(); $pictures = array(); $stats = array(); - -if ( isset( $_GET['dir'] ) && isset( $_POST['submit'] )) -{ - $pictures = get_images_without_thumbnail( $_GET['dir'] ); - // checking criteria - if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 ) - { - array_push( $errors, $lang['tn_err_width'].' 10' ); - } - if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 ) - { - array_push( $errors, $lang['tn_err_height'].' 10' ); - } - - // picture miniaturization - if ( count( $errors ) == 0 ) - { - $stats = phpwg_scandir( $_GET['dir'], $_POST['width'], $_POST['height'] ); - } -} - -//----------------------------------------------------- template initialization +// +-----------------------------------------------------------------------+ +// | template initialization | +// +-----------------------------------------------------------------------+ $template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') ); $template->assign_vars(array( 'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'], - 'L_UNLINK'=>$lang['tn_dirs_alone'], + 'L_UNLINK'=>$lang['tn_no_missing'], 'L_MISSING_THUMBNAILS'=>$lang['tn_dirs_alone'], 'L_RESULTS'=>$lang['tn_results_title'], - 'L_TN_PICTURE'=>$lang['tn_picture'], + 'L_PATH'=>$lang['path'], 'L_FILESIZE'=>$lang['filesize'], 'L_WIDTH'=>$lang['tn_width'], 'L_HEIGHT'=>$lang['tn_height'], @@ -273,182 +187,223 @@ $template->assign_vars(array( 'T_STYLE'=>$user['template'] )); +// +-----------------------------------------------------------------------+ +// | search pictures without thumbnails | +// +-----------------------------------------------------------------------+ +$wo_thumbnails = array(); +$thumbnalized = array(); -//----------------------------------------------------- miniaturization results -if ( sizeof( $errors ) != 0 ) +// what is the directory to search in ? +$query = ' +SELECT galleries_url + FROM '.SITES_TABLE.' + WHERE id = 1 +;'; +list($galleries_url) = mysql_fetch_array(pwg_query($query)); +$basedir = preg_replace('#/*$#', '', $galleries_url); + +$fs = get_fs($basedir); +// because isset is one hundred time faster than in_array +$fs['thumbnails'] = array_flip($fs['thumbnails']); + +foreach ($fs['elements'] as $path) { - $template->assign_block_vars('errors',array()); - for ( $i = 0; $i < sizeof( $errors ); $i++ ) + // only pictures need thumbnails + if (in_array(get_extension($path), $conf['picture_ext'])) { - $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i])); + $dirname = dirname($path); + $filename = basename($path); + // searching the element + $filename_wo_ext = get_filename_wo_extension($filename); + $tn_ext = ''; + $base_test = $dirname.'/thumbnail/'; + $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; + foreach ($conf['picture_ext'] as $ext) + { + if (isset($fs['thumbnails'][$base_test.$ext])) + { + $tn_ext = $ext; + break; + } + } + + if (empty($tn_ext)) + { + array_push($wo_thumbnails, $path); + } } } -else if (isset($_GET['dir']) and isset($_POST['submit']) and !empty($stats)) +// +-----------------------------------------------------------------------+ +// | thumbnails creation | +// +-----------------------------------------------------------------------+ +if (isset($_POST['submit'])) { + $errors = array(); $times = array(); - foreach ($stats as $stat) + $infos = array(); + + // checking criteria + if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10) { - array_push( $times, $stat['time'] ); + array_push($errors, $lang['tn_err_width'].' 10'); } - $sum=array_sum( $times ); - $average = $sum/sizeof($times); - sort( $times, SORT_NUMERIC ); - $max = array_pop($times); - $min = array_shift( $times); - - $template->assign_block_vars( - 'results', - array( - 'TN_NB'=>count( $stats ), - 'TN_TOTAL'=>number_format( $sum, 2, '.', ' ').' ms', - 'TN_MAX'=>number_format( $max, 2, '.', ' ').' ms', - 'TN_MIN'=>number_format( $min, 2, '.', ' ').' ms', - 'TN_AVERAGE'=>number_format( $average, 2, '.', ' ').' ms' - )); - if (!count($pictures)) + if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10) { - $template->assign_block_vars('warning',array()); + array_push($errors, $lang['tn_err_height'].' 10'); } - foreach ($stats as $i => $stat) + // picture miniaturization + if (count($errors) == 0) { - $class = ($i % 2)? 'row1':'row2'; - $color=''; - if ($stat['time'] == $max) + $num = 1; + foreach ($wo_thumbnails as $path) { - $color = 'red'; + if (is_numeric($_POST['n']) and $num > $_POST['n']) + { + break; + } + + $starttime = get_moment(); + if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg')) + { + $endtime = get_moment(); + $info['time'] = ($endtime - $starttime) * 1000; + array_push($infos, $info); + array_push($times, $info['time']); + array_push($thumbnalized, $path); + $num++; + } + else + { + break; + } } - else if ($stat['time'] == $min) + + if (count($infos) > 0) { - $color = '#33FF00'; - } - - $template->assign_block_vars( - 'results.picture', - array( - 'NB_IMG'=>($i+1), - 'FILE_IMG'=>$stat['file'], - 'FILESIZE_IMG'=>$stat['size'], - 'WIDTH_IMG'=>$stat['width'], - 'HEIGHT_IMG'=>$stat['height'], - 'TN_FILE_IMG'=>$stat['tn_file'], - 'TN_FILESIZE_IMG'=>$stat['tn_size'], - 'TN_WIDTH_IMG'=>$stat['tn_width'], - 'TN_HEIGHT_IMG'=>$stat['tn_height'], - 'GEN_TIME'=>number_format( $stat['time'], 2, '.', ' ').' ms', + $sum = array_sum($times); + $average = $sum / count($times); + sort($times, SORT_NUMERIC); + $max = array_pop($times); + if (count($thumbnalized) == 1) + { + $min = $max; + } + else + { + $min = array_shift($times); + } + + $template->assign_block_vars( + 'results', + array( + 'TN_NB'=>count($infos), + 'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms', + 'TN_MAX'=>number_format($max, 2, '.', ' ').' ms', + 'TN_MIN'=>number_format($min, 2, '.', ' ').' ms', + 'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms' + )); + + foreach ($infos as $i => $info) + { + if ($info['time'] == $max) + { + $class = 'worst_gen_time'; + } + else if ($info['time'] == $min) + { + $class = 'best_gen_time'; + } + else + { + $class = ''; + } - 'T_COLOR'=>$color, - 'T_CLASS'=>$class - )); + $template->assign_block_vars( + 'results.picture', + array( + 'PATH'=>$info['path'], + 'TN_FILE_IMG'=>$info['tn_file'], + 'TN_FILESIZE_IMG'=>$info['tn_size'], + 'TN_WIDTH_IMG'=>$info['tn_width'], + 'TN_HEIGHT_IMG'=>$info['tn_height'], + 'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms', + + 'T_CLASS'=>$class + )); + } + } + } +} +// +-----------------------------------------------------------------------+ +// | errors display | +// +-----------------------------------------------------------------------+ +if (count($errors) != 0) +{ + $template->assign_block_vars('errors',array()); + foreach ($errors as $error) + { + $template->assign_block_vars('errors.error',array('ERROR'=>$error)); } } -//-------------------------------------------------- miniaturization parameters -if (isset($_GET['dir']) and !sizeof($pictures)) +// +-----------------------------------------------------------------------+ +// | form & pictures without thumbnails display | +// +-----------------------------------------------------------------------+ +$remainings = array_diff($wo_thumbnails, $thumbnalized); + +if (count($remainings) > 0) { - $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&dir='.$_GET['dir']; - $gd = !empty( $_POST['gd'] )?$_POST['gd']:2; - $width = !empty( $_POST['width'] )?$_POST['width']:128; - $height = !empty( $_POST['height'] )?$_POST['height']:96; + $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail'; + $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2; + $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width']; + $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height']; + $n = !empty($_POST['n']) ? $_POST['n'] : 5; + $gdlabel = 'GD'.$gd.'_CHECKED'; + $nlabel = 'n_'.$n.'_CHECKED'; $template->assign_block_vars( 'params', array( 'F_ACTION'=>add_session_id($form_url), $gdlabel=>'checked="checked"', + $nlabel=>'checked="checked"', 'WIDTH_TN'=>$width, 'HEIGHT_TN'=>$height )); -//---------------------------------------------------------- remaining pictures - $pictures = get_images_without_thumbnail( $_GET['dir'] ); + $template->assign_block_vars( 'remainings', - array('TOTAL_IMG'=>count($pictures))); + array('TOTAL_IMG'=>count($remainings))); - foreach ($pictures as $i => $picture) + $num = 1; + foreach ($remainings as $path) { - $class = ($i % 2)? 'row1':'row2'; + $class = ($num % 2) ? 'row1' : 'row2'; + list($width, $height) = getimagesize($path); + $size = floor(filesize($path) / 1024).' KB'; + $template->assign_block_vars( 'remainings.remaining', array( - 'NB_IMG'=>($i+1), - 'FILE_TN'=>$picture['name'], - 'FILESIZE_IMG'=>$picture['size'], - 'WIDTH_IMG'=>$picture['width'], - 'HEIGHT_IMG'=>$picture['height'], - + 'NB_IMG'=>($num), + 'PATH'=>$path, + 'FILESIZE_IMG'=>$size, + 'WIDTH_IMG'=>$width, + 'HEIGHT_IMG'=>$height, + 'T_CLASS'=>$class )); + + $num++; } } -//-------------------------------------------------------------- directory list else { - $wo_thumbnails = array(); - - // what is the directory to search in ? - $query = ' -SELECT galleries_url - FROM '.SITES_TABLE.' - WHERE id = 1 -;'; - list($galleries_url) = mysql_fetch_array(pwg_query($query)); - $basedir = preg_replace('#/*$#', '', $galleries_url); - - $fs = get_fs($basedir); - // because isset is one hundred time faster than in_array - $fs['thumbnails'] = array_flip($fs['thumbnails']); - - foreach ($fs['elements'] as $path) - { - // only pictures need thumbnails - if (in_array(get_extension($path), $conf['picture_ext'])) - { - $dirname = dirname($path); - $filename = basename($path); - // searching the element - $filename_wo_ext = get_filename_wo_extension($filename); - $tn_ext = ''; - $base_test = $dirname.'/thumbnail/'; - $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; - foreach ($conf['picture_ext'] as $ext) - { - if (isset($fs['thumbnails'][$base_test.$ext])) - { - $tn_ext = $ext; - break; - } - } - - if (empty($tn_ext)) - { - if (!isset($wo_thumbnails[$dirname])) - { - $wo_thumbnails[$dirname] = 1; - } - else - { - $wo_thumbnails[$dirname]++; - } - } - } - } - - if (count($wo_thumbnails) > 0) - { - $template->assign_block_vars('directory_list', array()); - foreach ($wo_thumbnails as $directory => $nb_missing) - { - $url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail&dir='.$directory; - - $template->assign_block_vars( - 'directory_list.directory', - array( - 'DIRECTORY'=>$directory, - 'URL'=>add_session_id($url), - 'NB_MISSING'=>$nb_missing)); - } - } + $template->assign_block_vars('warning', array()); } +// +-----------------------------------------------------------------------+ +// | return to admin | +// +-----------------------------------------------------------------------+ $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail'); ?> |