source: trunk/admin/thumbnail.php @ 226

Last change on this file since 226 was 226, checked in by z0rglub, 21 years ago

Modifying ./include/isadmin.inc.php in ./admin/include/isadmin.inc.php since admin.php has moved to the PhpWebGallery root directory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
RevLine 
[2]1<?php
2/***************************************************************************
[26]3 *                              thumbnail.php                              *
[2]4 *                            -------------------                          *
[57]5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
[2]7 *                                                                         *
[57]8 *   $Id: thumbnail.php 226 2003-11-03 20:59:40Z z0rglub $
9 *                                                                         *
[2]10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
[226]19include_once( './admin/include/isadmin.inc.php' );
[26]20//------------------------------------------------------------------- functions
21// get_subdirs returns an array containing all sub directory names,
22// excepting : '.', '..' and 'thumbnail'.
23function get_subdirs( $dir )
[2]24{
[26]25  $sub_dirs = array();
26  if ( $opendir = opendir( $dir ) )
[2]27  {
[26]28    while ( $file = readdir( $opendir ) )
[2]29    {
[26]30      if ( $file != 'thumbnail' and $file != '.'
31           and $file != '..' and is_dir( $dir.'/'.$file ) )
[2]32      {
[26]33        array_push( $sub_dirs, $file );
[2]34      }
35    }
36  }
[26]37  return $sub_dirs;
[2]38}
39
[26]40// get_images_without_thumbnail returns an array with all the picture names
41// that don't have associated thumbnail in the directory. Each picture name
42// is associated with the width, heigh and filesize of the picture.
[2]43function get_images_without_thumbnail( $dir )
44{
[26]45  $images = array();
46  if ( $opendir = opendir( $dir ) )
[2]47  {
[26]48    while ( $file = readdir( $opendir ) )
[2]49    {
[26]50      $path = $dir.'/'.$file;
51      if ( is_image( $path, true ) )
[2]52      {
[26]53        if ( !TN_exists( $dir, $file ) )
[2]54        {
[26]55          $image_infos = getimagesize( $path );
56          $size = floor( filesize( $path ) / 1024 ). ' KB';
57          array_push( $images, array( 'name' => $file,
58                                      'width' => $image_infos[0],
59                                      'height' => $image_infos[1],
60                                      'size' => $size ) );
[2]61        }
62      }
63    }
64  }
65  return $images;
66}
[26]67
68// scandir scans a dir to find pictures without thumbnails. Once found,
69// creation of the thumbnails (RatioResizeImg). Only the first $_POST['n']
70// pictures without thumbnails are treated.
71// scandir returns an array with the generation time of each thumbnail (for
72// statistics purpose)
73function scandir( $dir, $width, $height )
[2]74{
[26]75  global $conf;
76  $stats = array();
77  if ( $opendir = opendir( $dir ) )
[2]78  {
[26]79    while ( $file = readdir ( $opendir ) )
[2]80    {
[26]81      $path = $dir.'/'.$file;
82      if ( is_image( $path, true ) )
[2]83      {
[26]84        if ( count( $stats ) < $_POST['n'] and !TN_exists( $dir, $file ) )
[2]85        {
[26]86          $starttime = get_moment();
87          $info = RatioResizeImg( $file, $width, $height, $dir.'/', 'jpg' );
88          $endtime = get_moment();
89          $info['time'] = ( $endtime - $starttime ) * 1000;
90          array_push( $stats, $info );
[2]91        }
92      }
93    }
94  }
[26]95  return $stats;
[2]96}
[26]97
98// RatioResizeImg creates a new picture (a thumbnail since it is supposed to
99// be smaller than original picture !) in the sub directory named
100// "thumbnail".
101function RatioResizeImg( $filename, $newWidth, $newHeight, $path, $tn_ext )
[2]102{
[26]103  global $conf, $lang;
104  // full path to picture
105  $filepath = $path.$filename;
106  // extension of the picture filename
107  $extension = get_extension( $filepath );
108  switch( $extension )
[2]109  {
[26]110  case 'jpg': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
111  case 'JPG': $srcImage = @imagecreatefromjpeg( $filepath ); break; 
112  case 'png': $srcImage = @imagecreatefrompng(  $filepath ); break; 
113  case 'PNG': $srcImage = @imagecreatefrompng(  $filepath ); break; 
114  default : unset( $extension ); break;
[2]115  }
116               
[26]117  if ( isset( $srcImage ) )
[2]118  {
[26]119    // width/height
120    $srcWidth    = imagesx( $srcImage ); 
121    $srcHeight   = imagesy( $srcImage ); 
122    $ratioWidth  = $srcWidth/$newWidth;
[2]123    $ratioHeight = $srcHeight/$newHeight;
[26]124
125    // maximal size exceeded ?
126    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
[2]127    {
[26]128      if ( $ratioWidth < $ratioHeight)
[2]129      { 
130        $destWidth = $srcWidth/$ratioHeight;
131        $destHeight = $newHeight; 
132      }
133      else
134      { 
135        $destWidth = $newWidth; 
136        $destHeight = $srcHeight/$ratioWidth;
137      }
138    }
139    else
140    {
141      $destWidth = $srcWidth;
142      $destHeight = $srcHeight;
143    }
[26]144    // according to the GD version installed on the server
145    if ( $_POST['gd'] == 2 )
[2]146    {
[26]147      // GD 2.0 or more recent -> good results (but slower)
[2]148      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
[26]149      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
150                          $destWidth,$destHeight,$srcWidth,$srcHeight );
[2]151    }
152    else
153    {
[26]154      // GD prior to version  2 -> pretty bad results :-/ (but fast)
[2]155      $destImage = imagecreate( $destWidth, $destHeight);
[26]156      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
157                        $destWidth,$destHeight,$srcWidth,$srcHeight );
[2]158    }
159                       
160                       
[26]161    if( !is_dir( $path.'thumbnail' ) )
[2]162    {
[26]163      umask( 0000 );
164      mkdir( $path.'thumbnail', 0777 );
[2]165    }
[26]166    $dest_file = $path.'thumbnail/'.$conf['prefix_thumbnail'];
167    $dest_file.= get_filename_wo_extension( $filename );
168    $dest_file.= '.'.$tn_ext;
[2]169                       
[26]170    // creation and backup of final picture
171    imagejpeg( $destImage, $dest_file );
172    // freeing memory ressources
[2]173    imagedestroy( $srcImage );
174    imagedestroy( $destImage );
175                       
[26]176    list( $width,$height ) = getimagesize( $filepath );
177    $size = floor( filesize( $filepath ) / 1024 ).' KB';
178    list( $tn_width,$tn_height ) = getimagesize( $dest_file );
179    $tn_size = floor( filesize( $dest_file ) / 1024 ).' KB';
180    $info = array( 'file'      => $filename,
181                   'width'     => $width,
182                   'height'    => $height,
183                   'size'      => $size,
184                   'tn_file'   => $dest_file,
185                   'tn_width'  => $tn_width,
186                   'tn_height' => $tn_height,
187                   'tn_size'   => $tn_size );
[2]188    return $info;
189  }
[26]190  // error
[2]191  else
192  {
193    echo $lang['tn_no_support']." ";
[26]194    if ( isset( $extenstion ) )
[2]195    {
[26]196      echo $lang['tn_format'].' '.$extension;
[2]197    }
198    else
199    {
200      echo $lang['tn_thisformat'];
201    }
202    exit();
203  }
204}
[26]205
206// array_max returns the highest value of the given array
[2]207function array_max( $array )
208{
[26]209  sort( $array, SORT_NUMERIC );
210  return array_pop( $array );
[2]211}
[26]212
213// array_min returns the lowest value of the given array
[2]214function array_min( $array )
215{
[26]216  sort( $array, SORT_NUMERIC );
217  return array_shift( $array );
[2]218}
[26]219
220// array_avg returns the average value of the array
221function array_avg( $array )
[2]222{
223  return array_sum( $array ) / sizeof( $array );
224}
225       
[26]226// get_displayed_dirs builds the tree of dirs under "galleries". If a
227// directory contains pictures without thumbnails, the become linked to the
228// page of thumbnails creation.
229function get_displayed_dirs( $dir, $indent )
[2]230{
[26]231  global $conf,$lang,$vtp,$sub;
[2]232               
[26]233  $sub_dirs = get_subdirs( $dir );
[2]234  // write of the dirs
[26]235  foreach ( $sub_dirs as $sub_dir ) {
236    $pictures = get_images_without_thumbnail( $dir.'/'.$sub_dir );
237    $vtp->addSession( $sub, 'dir' );
238    $vtp->setVar( $sub, 'dir.indent', $indent );
239    if ( count( $pictures ) > 0 )
[2]240    {
[26]241      $vtp->addSession( $sub, 'linked' );
242      $url = './admin.php?page=thumbnail&amp;dir='.$dir."/".$sub_dir;
243      $vtp->setVar( $sub, 'linked.url', add_session_id( $url ) );
244      $vtp->setVar( $sub, 'linked.name', $sub_dir );
245      $vtp->setVar( $sub, 'linked.nb_pic', count( $pictures ) );
246      $vtp->closeSession( $sub, 'linked' );
[2]247    }
[26]248    else
[2]249    {
[26]250      $vtp->addSession( $sub, 'unlinked' );
251      $vtp->setVar( $sub, 'unlinked.name', $sub_dir );
252      $vtp->closeSession( $sub, 'unlinked' );
[2]253    }
[26]254    $vtp->closeSession( $sub, 'dir' );
255    // recursive call
256    $dirs.= get_displayed_dirs( $dir.'/'.$sub_dir,
257                                $indent+30 );
258   
[2]259  }
260}
[26]261//----------------------------------------------------- template initialization
[225]262$sub = $vtp->Open( './template/'.$user['template'].'/admin/thumbnail.vtp' );
[26]263$tpl = array(
264  'tn_dirs_title','tn_dirs_alone','tn_params_title','tn_params_GD',
265  'tn_params_GD_info','tn_width','tn_params_width_info','tn_height',
266  'tn_params_height_info','tn_params_create','tn_params_create_info',
267  'tn_params_format','tn_params_format_info','submit','tn_alone_title',
268  'filesize','tn_picture','tn_results_title','thumbnail',
269  'tn_results_gen_time','tn_stats','tn_stats_nb','tn_stats_total',
270  'tn_stats_max','tn_stats_min','tn_stats_mean' );
271templatize_array( $tpl, 'lang', $sub );
[29]272$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
[26]273//----------------------------------------------------- miniaturization results
274if ( isset( $_GET['dir'] ) )
[2]275{
[26]276  $pictures = get_images_without_thumbnail( $_GET['dir'] );
277  if ( count( $pictures ) == 0 )
[2]278  {
[26]279    $vtp->addSession( $sub, 'warning' );
280    $vtp->closeSession( $sub, 'warning' );
[2]281  }
[26]282  elseif ( isset( $_POST['submit'] ) )
[2]283  {
[26]284    // checking criteria
285    $errors = array();
286    if ( !ereg( "^[0-9]{2,3}$", $_POST['width'] ) or $_POST['width'] < 10 )
[2]287    {
[26]288      array_push( $errors, $lang['tn_err_width'].' 10' );
[2]289    }
[26]290    if ( !ereg( "^[0-9]{2,3}$", $_POST['height'] ) or $_POST['height'] < 10 )
[2]291    {
[26]292      array_push( $errors, $lang['tn_err_height'].' 10' );
[2]293    }
[26]294    // picture miniaturization
295    if ( count( $errors ) == 0 )
[2]296    {
[26]297      $vtp->addSession( $sub, 'results' );
298      $stats = scandir( $_GET['dir'], $_POST['width'], $_POST['height'] );
299      $times = array();
300      foreach ( $stats as $stat ) {
301        array_push( $times, $stat['time'] );
[2]302      }
[26]303      $max = array_max( $times );
304      $min = array_min( $times );
305      foreach ( $stats as $i => $stat ) {
306        $vtp->addSession( $sub, 'picture' );
307        if ( $i % 2 == 1 )
[2]308        {
[26]309          $vtp->setVar( $sub, 'picture.class', 'row2' );
[2]310        }
[26]311        $vtp->setVar( $sub, 'picture.num',            ($i+1) );
312        $vtp->setVar( $sub, 'picture.file',           $stat['file'] );
313        $vtp->setVar( $sub, 'picture.filesize',       $stat['size'] );
314        $vtp->setVar( $sub, 'picture.width',          $stat['width'] );
315        $vtp->setVar( $sub, 'picture.height',         $stat['height'] );
316        $vtp->setVar( $sub, 'picture.thumb_file',     $stat['tn_file'] );
317        $vtp->setVar( $sub, 'picture.thumb_filesize', $stat['tn_size'] );
318        $vtp->setVar( $sub, 'picture.thumb_width',    $stat['tn_width'] );
319        $vtp->setVar( $sub, 'picture.thumb_height',   $stat['tn_height'] );
320        $vtp->setVar( $sub, 'picture.time',
321                      number_format( $stat['time'], 2, '.', ' ').' ms' );
322        if ( $stat['time'] == $max )
[2]323        {
[26]324          $vtp->setVar( $sub, 'picture.color', 'red' );
[2]325        }
[26]326        else if ( $stat['time'] == $min )
[2]327        {
[26]328          $vtp->setVar( $sub, 'picture.color', 'green' );
[2]329        }
[26]330        $vtp->closeSession( $sub, 'picture' );
[2]331      }
[26]332      // general statistics
333      $vtp->setVar( $sub, 'results.stats_nb', count( $stats ) );
334      $vtp->setVar( $sub, 'results.stats_total',
335                    number_format( array_sum( $times ), 2, '.', ' ').' ms' );
336      $vtp->setVar( $sub, 'results.stats_max',
337                    number_format( $max, 2, '.', ' ').' ms' );
338      $vtp->setVar( $sub, 'results.stats_min',
339                    number_format( $min, 2, '.', ' ').' ms' );
340      $vtp->setVar( $sub, 'results.stats_mean',
341                    number_format( array_avg( $times ), 2, '.', ' ').' ms' );
342      $vtp->closeSession( $sub, 'results' );
[2]343    }
344    else
345    {
[26]346      $vtp->addSession( $sub, 'errors' );
347      foreach ( $errors as $error ) {
348        $vtp->addSession( $sub, 'li' );
349        $vtp->setVar( $sub, 'li.li', $error );
350        $vtp->closeSession( $sub, 'li' );
351      }
352      $vtp->closeSession( $sub, 'errors' );
[2]353    }
354  }
[26]355//-------------------------------------------------- miniaturization parameters
356  if ( sizeof( $pictures ) != 0 )
[2]357  {
[26]358    $vtp->addSession( $sub, 'params' );
359    $url = './admin.php?page=thumbnail&amp;dir='.$_GET['dir'];
360    $vtp->setVar( $sub, 'params.action', add_session_id( $url ) );
361    // GD version selected...
362    if ( $_POST['gd'] == 1 )
[2]363    {
[26]364      $vtp->setVar( $sub, 'params.gd1_checked', ' checked="checked"' );
[2]365    }
[26]366    else
[2]367    {
[26]368      $vtp->setVar( $sub, 'params.gd2_checked', ' checked="checked"' );
[2]369    }
[26]370    // width values
371    if ( isset( $_POST['width'] ) )
372    {
373      $vtp->setVar( $sub, 'params.width_value', $_POST['width'] );
374    }
[2]375    else
376    {
[26]377      $vtp->setVar( $sub, 'params.width_value', '128' );
[2]378    }
[26]379    // height value
380    if ( isset( $_POST['height'] ) )
[2]381    {
[26]382      $vtp->setVar( $sub, 'params.height_value', $_POST['height'] );
[2]383    }
384    else
385    {
[26]386      $vtp->setVar( $sub, 'params.height_value', '96' );
[2]387    }
[26]388    // options for the number of picture to miniaturize : "n"
389    $options = array( 5,10,20,40 );
390    foreach ( $options as $option ) {
391      $vtp->addSession( $sub, 'n_option' );
392      $vtp->setVar( $sub, 'n_option.option', $option );
393      if ( $option == $_POST['n'] )
[2]394      {
[26]395        $vtp->setVar( $sub, 'n_option.selected', ' selected="selected"' );
[2]396      }
[26]397      $vtp->closeSession( $sub, 'n_option' );
[2]398    }
[26]399    $vtp->closeSession( $sub, 'params' );
400//---------------------------------------------------------- remaining pictures
401    $vtp->addSession( $sub, 'remainings' );
402    $pictures = get_images_without_thumbnail( $_GET['dir'] );
403    $vtp->setVar( $sub, 'remainings.total', count( $pictures ) );
404    foreach ( $pictures as $i => $picture ) {
405      $vtp->addSession( $sub, 'remaining' );
406      if ( $i % 2 == 1 )
407      {
408        $vtp->setVar( $sub, 'remaining.class', 'row2' );
409      }
410      $vtp->setVar( $sub, 'remaining.num',      ($i+1) );
411      $vtp->setVar( $sub, 'remaining.file',     $picture['name'] );
412      $vtp->setVar( $sub, 'remaining.filesize', $picture['size'] );
413      $vtp->setVar( $sub, 'remaining.width',    $picture['width'] );
414      $vtp->setVar( $sub, 'remaining.height',   $picture['height'] );
415      $vtp->closeSession( $sub, 'remaining' );
416    }
417    $vtp->closeSession( $sub, 'remainings' );
[2]418  }
419}
[26]420//-------------------------------------------------------------- directory list
[2]421else
422{
[26]423  $vtp->addSession( $sub, 'directory_list' );
424  get_displayed_dirs( '../galleries', 60 );
425  $vtp->closeSession( $sub, 'directory_list' );
[2]426}
[26]427//----------------------------------------------------------- sending html code
428$vtp->Parse( $handle , 'sub', $sub );
[2]429?>
Note: See TracBrowser for help on using the repository browser.