Ignore:
Timestamp:
Apr 22, 2011, 3:19:36 PM (13 years ago)
Author:
patdenice
Message:

feature:2274
Create thumbnail through ajax.
Remove $conftn_width, $conftn_height and $conftn_compression_level parameters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/thumbnail.php

    r8728 r10570  
    2323
    2424include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     25include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     26
     27check_status(ACCESS_ADMINISTRATOR);
    2528
    2629// +-----------------------------------------------------------------------+
    27 // | Check Access and exit when user status is not ok                      |
     30// |                          Load configuration                           |
    2831// +-----------------------------------------------------------------------+
    29 check_status(ACCESS_ADMINISTRATOR);
     32prepare_upload_configuration();
    3033
    31 //------------------------------------------------------------------- functions
    32 // RatioResizeImg creates a new picture (a thumbnail since it is supposed to
    33 // be smaller than original picture !) in the sub directory named
    34 // "thumbnail".
    35 function RatioResizeImg($info, $path, $newWidth, $newHeight, $tn_ext)
     34$upload_form_config = get_upload_form_config();
     35
     36$form_values = array();
     37
     38foreach ($upload_form_config as $param_shortname => $param)
    3639{
    37   global $conf, $lang, $page;
    38 
    39   if ($info !== false)
    40   {
    41     //someone hooked us - so we skip
    42     return $info;
    43   }
    44 
    45   if (!function_exists('gd_info'))
    46   {
    47     return;
    48   }
    49 
    50   $filename = basename($path);
    51   $dirname = dirname($path);
    52  
    53   // extension of the picture filename
    54   $extension = get_extension($filename);
    55 
    56   if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
    57   {
    58     $srcImage = @imagecreatefromjpeg($path);
    59   }
    60   else if ($extension == 'png' or $extension == 'PNG')
    61   {
    62     $srcImage = @imagecreatefrompng($path);
    63   }
    64   else
    65   {
    66     unset($extension);
    67   }
    68 
    69   if ( isset( $srcImage ) )
    70   {
    71     // width/height
    72     $srcWidth    = imagesx( $srcImage );
    73     $srcHeight   = imagesy( $srcImage );
    74     $ratioWidth  = $srcWidth/$newWidth;
    75     $ratioHeight = $srcHeight/$newHeight;
    76 
    77     // maximal size exceeded ?
    78     if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
    79     {
    80       if ( $ratioWidth < $ratioHeight)
    81       {
    82         $destWidth = $srcWidth/$ratioHeight;
    83         $destHeight = $newHeight;
    84       }
    85       else
    86       {
    87         $destWidth = $newWidth;
    88         $destHeight = $srcHeight/$ratioWidth;
    89       }
    90     }
    91     else
    92     {
    93       $destWidth = $srcWidth;
    94       $destHeight = $srcHeight;
    95     }
    96     // according to the GD version installed on the server
    97     if ( $_POST['gd'] == 2 )
    98     {
    99       // GD 2.0 or more recent -> good results (but slower)
    100       $destImage = imagecreatetruecolor( $destWidth, $destHeight);
    101       imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
    102                           $destWidth,$destHeight,$srcWidth,$srcHeight );
    103     }
    104     else
    105     {
    106       // GD prior to version  2 -> pretty bad results :-/ (but fast)
    107       $destImage = imagecreate( $destWidth, $destHeight);
    108       imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
    109                         $destWidth,$destHeight,$srcWidth,$srcHeight );
    110     }
    111 
    112     if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false)
    113     {
    114       return false;
    115     }
    116 
    117     $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
    118     $dest_file.= get_filename_wo_extension($filename);
    119     $dest_file.= '.'.$tn_ext;
    120    
    121     // creation and backup of final picture
    122     if (!is_writable($tndir))
    123     {
    124       array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access'));
    125       return false;
    126     }
    127     imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
    128     // freeing memory ressources
    129     imagedestroy( $srcImage );
    130     imagedestroy( $destImage );
    131    
    132     list($tn_width, $tn_height) = getimagesize($dest_file);
    133     $tn_size = floor(filesize($dest_file) / 1024).' KB';
    134    
    135     $info = array( 'path'      => $path,
    136                    'tn_file'   => $dest_file,
    137                    'tn_width'  => $tn_width,
    138                    'tn_height' => $tn_height,
    139                    'tn_size'   => $tn_size );
    140     return $info;
    141   }
    142   // error
    143   else
    144   {
    145     echo l10n('Photo unreachable or no support')." ";
    146     if ( isset( $extension ) )
    147     {
    148       echo l10n('for the file format').' '.$extension;
    149     }
    150     else
    151     {
    152       echo l10n('for this file format');
    153     }
    154     exit();
    155   }
     40  $param_name = 'upload_form_'.$param_shortname;
     41  $form_values[$param_shortname] = $conf[$param_name];
    15642}
    15743
    158 $pictures = array();
    159 $stats = array();
    160 
    161 if (!function_exists('gd_info'))
    162 {
    163   array_push($page['errors'], l10n('GD library is missing'));
    164 }
    165 
    166 // add default event handler for thumbnail resize
    167 add_event_handler('thumbnail_resize', 'RatioResizeImg', EVENT_HANDLER_PRIORITY_NEUTRAL, 5);
    168 
    169 // +-----------------------------------------------------------------------+
    170 // |                       template initialization                         |
    171 // +-----------------------------------------------------------------------+
    172 $template->set_filenames( array('thumbnail'=>'thumbnail.tpl') );
    173 
    174 $template->assign(
    175   array('U_HELP' => get_root_url().'admin/popuphelp.php?page=thumbnail')
    176   );
    17744// +-----------------------------------------------------------------------+
    17845// |                   search pictures without thumbnails                  |
    17946// +-----------------------------------------------------------------------+
    18047$wo_thumbnails = array();
    181 $thumbnalized = array();
    18248
    18349// what is the directory to search in ?
     
    23197  } // next element
    23298} // next site id
    233 // +-----------------------------------------------------------------------+
    234 // |                         thumbnails creation                           |
    235 // +-----------------------------------------------------------------------+
    236 if (isset($_POST['submit']))
    237 {
    238   $times = array();
    239   $infos = array();
    240  
    241   // checking criteria
    242   if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10)
    243   {
    244     array_push($page['errors'], l10n('width must be a number superior to').' 10');
    245   }
    246   if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10)
    247   {
    248     array_push($page['errors'], l10n('height must be a number superior to').' 10');
    249   }
    250  
    251   // picture miniaturization
    252   if (count($page['errors']) == 0)
    253   {
    254     $num = 1;
    255     foreach ($wo_thumbnails as $path)
    256     {
    257       if (is_numeric($_POST['n']) and $num > $_POST['n'])
    258       {
    259         break;
    260       }
    261      
    262       $starttime = get_moment();
    263       if ($info = trigger_event('thumbnail_resize',
    264             false,
    265             $path,
    266             $_POST['width'],
    267             $_POST['height'],
    268             'jpg'
    269             )
    270          )
    271       {
    272         $endtime = get_moment();
    273         $info['time'] = ($endtime - $starttime) * 1000;
    274         array_push($infos, $info);
    275         array_push($times, $info['time']);
    276         array_push($thumbnalized, $path);
    277         $num++;
    278       }
    279       else
    280       {
    281         break;
    282       }
    283     }
    28499
    285     if (count($infos) > 0)
    286     {
    287       $sum = array_sum($times);
    288       $average = $sum / count($times);
    289       sort($times, SORT_NUMERIC);
    290       $max = array_pop($times);
    291       if (count($thumbnalized) == 1)
    292       {
    293         $min = $max;
    294       }
    295       else
    296       {
    297         $min = array_shift($times);
    298       }
    299      
    300       $tpl_var =
    301         array(
    302           'TN_NB'=>count($infos),
    303           'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
    304           'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
    305           'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
    306           'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms',
    307           'elements' => array()
    308           );
    309      
    310       foreach ($infos as $i => $info)
    311       {
    312         $tpl_var['elements'][] =
    313           array(
    314             'PATH'=>$info['path'],
    315             'TN_FILE_IMG'=>$info['tn_file'],
    316             'TN_FILESIZE_IMG'=>$info['tn_size'],
    317             'TN_WIDTH_IMG'=>$info['tn_width'],
    318             'TN_HEIGHT_IMG'=>$info['tn_height'],
    319             'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
    320             );
    321       }
    322       $template->assign('results', $tpl_var);
    323     }
    324   }
    325 }
    326100// +-----------------------------------------------------------------------+
    327101// |             form & pictures without thumbnails display                |
    328102// +-----------------------------------------------------------------------+
    329 $remainings = array_diff($wo_thumbnails, $thumbnalized);
    330 
    331 if (count($remainings) > 0)
     103if (count($wo_thumbnails) > 0)
    332104{
    333   $form_url = get_root_url().'admin.php?page=thumbnail';
    334   $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
    335   $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
    336   $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
    337   $n = !empty($_POST['n']) ? $_POST['n'] : 5;
    338  
    339   $template->assign(
    340     'params',
    341     array(
    342       'F_ACTION'=> $form_url,
    343       'GD_SELECTED' => $gd,
    344       'N_SELECTED' => $n,
    345       'WIDTH_TN'=>$width,
    346       'HEIGHT_TN'=>$height
    347       ));
    348 
    349   $template->assign(
    350     'TOTAL_NB_REMAINING',
    351     count($remainings));
    352 
    353   foreach ($remainings as $path)
     105  foreach ($wo_thumbnails as $path)
    354106  {
    355107    list($width, $height) = getimagesize($path);
     
    367119}
    368120
     121foreach (array_keys($upload_form_config) as $field)
     122{
     123  if (is_bool($upload_form_config[$field]['default']))
     124  {
     125    $form_values[$field] = $form_values[$field] ? 'checked="checked"' : '';
     126  }
     127}
     128
     129$template->assign(
     130  array(
     131    'F_ACTION' => get_root_url().'admin.php?page=thumbnail',
     132    'values' => $form_values,
     133    'TOTAL_NB_REMAINING' => count($wo_thumbnails),
     134  )
     135);
     136
    369137// +-----------------------------------------------------------------------+
    370138// |                           return to admin                             |
    371139// +-----------------------------------------------------------------------+
     140$template->set_filenames( array('thumbnail'=>'thumbnail.tpl') );
     141
     142$template->assign('U_HELP', get_root_url().'admin/popuphelp.php?page=thumbnail');
     143
    372144$template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
    373145?>
Note: See TracChangeset for help on using the changeset viewer.