source: trunk/admin/thumbnail.php @ 19

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

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1<?php
2/***************************************************************************
3 *                 thumbnail.php is a part of PhpWebGallery                *
4 *                            -------------------                          *
5 *   last update          : Thursday, July 25, 2002                        *
6 *   email                : pierrick@z0rglub.com                           *
7 *                                                                         *
8 ***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17include_once( "./include/isadmin.inc.php" );
18       
19function get_subdirs( $rep )
20{
21  $sub_rep = array();
22  $i = 0;
23  if ( $opendir = opendir ( $rep ) )
24  {
25    while ( $file = readdir ( $opendir ) )
26    {
27      if ( $file != "thumbnail" && $file != "."
28           && $file != ".." && is_dir ( $rep.$file ) )
29      {
30        $sub_rep[$i++] = $file;
31      }
32    }
33  }
34  return $sub_rep;
35}
36
37/*
38        $tab_ext = array ( 'jpg', 'JPG','png','PNG' );
39        $tab_tn_ext = array ( 'jpg', 'JPG','png','PNG', 'gif', 'GIF' );
40*/
41function get_images_without_thumbnail( $dir )
42{
43  $i = 0;
44  if ( $opendir = opendir ( $dir ) )
45  {
46    while ( $file = readdir ( $opendir ) )
47    {
48      $lien_image = $dir."/".$file;
49      if ( is_image( $lien_image, true ) )
50      {
51        if ( !TN_exist( $dir, $file ) )
52        {
53          $taille_image = getimagesize( $lien_image );
54          $size = floor ( filesize( $lien_image ) / 1024 ). " KB";
55          $images[$i++] = array(        'name' => $file,
56                                        'width' => $taille_image[0],
57                                        'height' => $taille_image[1],
58                                        'size' => $size
59            );
60        }
61      }
62    }
63  }
64  return $images;
65}
66       
67function scandir( $DIR, $width, $height )
68{
69  global $HTTP_POST_VARS, $conf, $output;
70  $compteur = 0;
71  $temps = array();
72  if ( $ODIR = opendir( $DIR ) )
73  {
74    while ( $FILE = readdir ( $ODIR ) )
75    {
76      $TMP = $DIR."/".$FILE;
77      if ( is_image ( $TMP, true ) )
78      {
79        if ( $compteur < $HTTP_POST_VARS['n'] && !TN_exist( $DIR, $FILE ) )
80        {
81          $t1 = explode( " ", microtime() );
82          $t2 = explode( ".", $t1[0] );
83          $t2 = $t1[1].".".$t2[1];
84          $info = RatioResizeImg( $FILE, $width, $height, $DIR."/", "jpg" );
85          $t3 = explode( " ", microtime() );
86          $t4 = explode( ".", $t3[0] );
87          $t4 = $t3[1].".".$t4[1];
88          $info['temps'] = ( $t4 - $t2 ) * 1000;
89          $temps[$compteur++] = $info;
90          //$output.= " (".number_format( $temps[$compteur-1], 2, '.', ' ')." ms)<br />";;
91        }
92      }
93    }
94  }
95  return $temps;
96}
97       
98function RatioResizeImg( $image, $newWidth, $newHeight, $path, $extension)
99{
100  global $conf, $HTTP_POST_VARS;
101  // chemin complet de l'image :
102  $chemin = $path.$image;
103  // détéction du type de l'image
104  eregi( "(...)$", $chemin, $regs);
105  $type = $regs[1];
106  switch( $type )
107  {
108  case "jpg": $srcImage = @imagecreatefromjpeg( $chemin ); break; 
109  case "JPG": $srcImage = @imagecreatefromjpeg( $chemin ); break; 
110  case "png": $srcImage = @imagecreatefrompng( $chemin ); break; 
111  case "PNG": $srcImage = @imagecreatefrompng( $chemin ); break; 
112  default : unset( $type ); break;
113  }
114               
115  if( $srcImage )
116  {
117    // hauteurs/largeurs
118    $srcWidth = imagesx( $srcImage ); 
119    $srcHeight = imagesy( $srcImage ); 
120    $ratioWidth = $srcWidth/$newWidth;
121    $ratioHeight = $srcHeight/$newHeight;
122                       
123    // taille maximale dépassée ?
124    if (($ratioWidth > 1) || ($ratioHeight > 1))
125    {
126      if( $ratioWidth < $ratioHeight)
127      { 
128        $destWidth = $srcWidth/$ratioHeight;
129        $destHeight = $newHeight; 
130      }
131      else
132      { 
133        $destWidth = $newWidth; 
134        $destHeight = $srcHeight/$ratioWidth;
135      }
136    }
137    else
138    {
139      $destWidth = $srcWidth;
140      $destHeight = $srcHeight;
141    }
142    // selon votre version de GD installée sur le serveur hébergeur
143    if ( $HTTP_POST_VARS['gd'] == 2 )
144    {
145      // Partie 1 : GD 2.0 ou supérieur, résultat très bons
146      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
147      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, $destWidth,$destHeight,$srcWidth,$srcHeight );
148    }
149    else
150    {
151      // Partie 2 : GD inférieur à 2, résultat très moyens
152      $destImage = imagecreate( $destWidth, $destHeight);
153      imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, $destWidth,$destHeight,$srcWidth,$srcHeight );
154    }
155                       
156                       
157    if( !is_dir( $path."thumbnail" ) )
158    {
159      umask(0000);
160      mkdir( $path."thumbnail", 0777 );
161    }
162    $dest_file  = $path."thumbnail/".$conf['prefix_thumbnail'].substr ( $image, 0, strrpos ( $image, ".") ).".".$extension;
163                       
164    // création et sauvegarde de l'image finale
165    imagejpeg($destImage, $dest_file);
166    // libère la mémoire
167    imagedestroy( $srcImage );
168    imagedestroy( $destImage );
169                       
170    // renvoit l'URL de l'image
171    //return $dest_file;
172    $taille_image = getimagesize( $chemin );
173    $size = number_format( floor ( filesize( $chemin ) / 1024 ), 0, '', ' ')." KB";
174    $tn_taille_image = getimagesize( $dest_file );
175    $tn_size = number_format( floor ( filesize( $dest_file ) ), 0, '', ' ')." octets";
176    $info = array(      'name' => $image,
177                        'width' => $taille_image[0],
178                        'height' => $taille_image[1],
179                        'size' => $size,
180                        'tn_name' => $conf['prefix_thumbnail'].substr ( $image, 0, strrpos ( $image, ".") ).".".$extension,
181                        'tn_width' => $tn_taille_image[0],
182                        'tn_height' => $tn_taille_image[1],
183                        'tn_size' => $tn_size
184      );
185    return $info;
186  }
187  // erreur
188  else
189  {
190    echo $lang['tn_no_support']." ";
191    if ($type)
192    {
193      echo $lang['tn_format']." $type";
194    }
195    else
196    {
197      echo $lang['tn_thisformat'];
198    }
199    exit();
200  }
201}
202       
203function array_max( $array )
204{
205  $max = 0;
206  for ( $i = 0; $i < sizeof( $array ); $i++ )
207  {
208    if ( $array[$i] > $max )
209    {
210      $max = $array[$i];
211    }
212  }
213  return $max;
214}
215       
216function array_min( $array )
217{
218  $min = 99999999999999;
219  for ( $i = 0; $i < sizeof( $array ); $i++ )
220  {
221    if ( $array[$i] < $min )
222    {
223      $min = $array[$i];
224    }
225  }
226  return $min;
227}
228       
229function array_moy( $array )
230{
231  return array_sum( $array ) / sizeof( $array );
232}
233       
234// get_dirs retourne un tableau contenant tous les sous-répertoires d'un répertoire
235function get_displayed_dirs( $rep, $indent )
236{
237  global $conf,$lang;
238               
239  $sub_rep = array();
240  $i = 0;
241  $dirs = "";
242  if ( $opendir = opendir ( $rep ) )
243  {
244    while ( $file = readdir ( $opendir ) )
245    {
246      if ( $file != "." && $file != ".." && is_dir ( $rep."/".$file ) && $file != "thumbnail" )
247      {
248        $sub_rep[$i++] = $file;
249      }
250    }
251  }
252  // write of the dirs
253  for ( $i = 0; $i < sizeof( $sub_rep ); $i++ )
254  {
255    $images = get_images_without_thumbnail( $rep."/".$sub_rep[$i] );
256    $nb_picture_without_TN = sizeof( $images );
257    $dirs.= $indent;
258    if ( $nb_picture_without_TN > 0 )
259    {
260      $dirs.= "<a href=\"".add_session_id_to_url( "./admin.php?page=thumbnail&amp;dir=".$rep."/".$sub_rep[$i] )."\">";
261    }
262    $dirs.= "<img src=\"".$conf['lien_puce']."\" style=\"border:none;\" alt=\"&gt;\"/>".$sub_rep[$i];
263    if ( $nb_picture_without_TN > 0 )
264    {
265      $dirs.= "</a>";
266    }
267    if ( $nb_picture_without_TN > 0 )
268    {
269      $dirs.= " [ $nb_picture_without_TN ".$lang['tn_dirs_alone']." ]";
270    }
271    $dirs.= "<br />";
272    $dirs.= get_displayed_dirs( $rep."/".$sub_rep[$i], $indent."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" );
273  }
274  return $dirs;         
275}
276
277$output = "";
278       
279if ( isset( $HTTP_GET_VARS['dir'] ) )
280{
281  //---------------vérification de la présence d'images sans thumbnail
282  $images = get_images_without_thumbnail( $HTTP_GET_VARS['dir'] );
283  if ( sizeof( $images ) == 0 )
284  {
285    $output.= "<div style=\"text-align:center;font-weight:bold;margin:10px;\"> [ 0 ".$lang['tn_dirs_alone']." ]</div>";
286  }
287  else if ( isset( $HTTP_POST_VARS['submit'] ) )
288  {
289    //----------------------------------------vérification des variables
290    $nb_erreur = 0;
291    $erreur = "";
292    if ( !ereg( "^[0-9]{2,3}$", $HTTP_POST_VARS['width'] ) || $HTTP_POST_VARS['width'] < 10 )
293    {
294      $nb_erreur++;
295      $erreur.= "<li>".$lang['tn_err_width']." 10</li>";
296    }
297    if ( !ereg( "^[0-9]{2,3}$", $HTTP_POST_VARS['height'] ) || $HTTP_POST_VARS['height'] < 10 )
298    {
299      $nb_erreur++;
300      $erreur.= "<li>".$lang['tn_err_height']." 10</li>";
301    }
302    if ( !isset( $HTTP_POST_VARS['gd'] ) )
303    {
304      $nb_erreur++;
305      $erreur.= "<li>".$lang['tn_err_GD']."</li>";
306    }
307                       
308    //---------------------------------------------listing des résultats
309    if ( $nb_erreur == 0 )
310    {
311      $style = "class=\"row2\" style=\"text-align:center;font-weight:bold;";
312      $output.= "
313        <table style=\"width:100%;\">
314                <tr>
315                        <th colspan=\"10\">".$lang['tn_results_title']."</th>
316                </tr>
317                <tr>
318                        <td ".$style."\">&nbsp;</td>
319                        <td ".$style."\">".$lang['tn_picture']."</td>
320                        <td ".$style."\">".$lang['tn_filesize']."</td>
321                        <td ".$style."\">".$lang['tn_width']."</td>
322                        <td ".$style."\">".$lang['tn_height']."</td>
323                        <td ".$style."background-color:#D3DCE3;\">".$lang['tn_results_gen_time']."</td>
324                        <td ".$style."\">".$lang['thumbnail']."</td>
325                        <td ".$style."\">".$lang['tn_filesize']."</td>
326                        <td ".$style."\">".$lang['tn_width']."</td>
327                        <td ".$style."\">".$lang['tn_height']."</td>
328                </tr>";
329      $tab_infos = scandir( $HTTP_GET_VARS['dir'], $HTTP_POST_VARS['width'], $HTTP_POST_VARS['height'] );
330      for ( $i = 0; $i < sizeof ( $tab_infos ); $i++ )
331      {
332        $temps[$i] = $tab_infos[$i]['temps'];
333      }
334      $max = array_max( $temps );
335      $min = array_min( $temps );
336      for ( $i = 0; $i < sizeof ( $tab_infos ); $i++ )
337      {
338        $temps[$i] = $tab_infos[$i]['temps'];
339        $num = $i + 1;
340        $class = "";
341        if ( $i%2 == 1 )
342        {
343          $class = "class=\"row2\"";
344        }
345        $output.= "
346                <tr>
347                        <td class=\"row2\">$num</td>
348                        <td $class>".$tab_infos[$i]['name']."</td>
349                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['size']."</td>
350                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['width']."</td>
351                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['height']."</td>
352                        <th><div style=\"text-align:right;margin-right:5px;";
353        if ( $tab_infos[$i]['temps'] == $max )
354        {
355          $output.= "color:red;";
356        }
357        if ( $tab_infos[$i]['temps'] == $min )
358        {
359          $output.= "color:green;";
360        }
361        $output.= "\">".number_format( $tab_infos[$i]['temps'], 2, '.', ' ')." ms</div></th>
362                        <td $class>".$tab_infos[$i]['tn_name']."</td>
363                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['tn_size']."</td>
364                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['tn_width']."</td>
365                        <td $class style=\"text-align:right;\">".$tab_infos[$i]['tn_height']."</td>
366                </tr>";
367      }
368      $output.= "
369                <tr>
370                        <td colspan=\"10\">&nbsp;</td>
371                </tr>
372        </table>
373        <table style=\"margin:auto;border:1px solid black;\">
374                <tr>
375                        <td colspan=\"2\" style=\"text-align:center;font-weight:bold;\" class=\"row2\">".$lang['tn_stats']."</td>
376                </tr>
377                <tr>
378                        <td>".$lang['tn_stats_nb']." : </td>
379                        <td style=\"text-align:center;\">".sizeof( $temps )."</td>
380                </tr>
381                <tr>
382                        <td>".$lang['tn_stats_total']." : </td>
383                        <td style=\"text-align:right;\">".number_format( array_sum( $temps ), 2, '.', ' ')." ms</td>
384                </tr>
385                <tr>
386                        <td>".$lang['tn_stats_max']." : </td>
387                        <td style=\"text-align:right;\">".number_format( $max, 2, '.', ' ')." ms</td>
388                </tr>
389                <tr>
390                        <td>".$lang['tn_stats_min']." : </td>
391                        <td style=\"text-align:right;\">".number_format( $min, 2, '.', ' ')." ms</td>
392                </tr>
393                <tr>
394                        <td>".$lang['tn_stats_mean']." : </td>
395                        <td style=\"text-align:right;\">".number_format( array_moy( $temps ), 2, '.', ' ')." ms</td>
396                </tr>
397        </table>
398        <table>
399                <tr>
400                        <td>&nbsp;</td>
401                </tr>
402        </table>";
403    }
404    else
405    {
406      $output.= "
407        <div class=\"erreur\" style=\"margin-top:10px;\">".$lang['tn_err']."</div>
408        <div class=\"erreur\" style=\"text-align:left;margin-left:20px;\">
409                <ul>
410                        $erreur
411                </ul>
412        </div>";
413    }
414  }
415  //-------------------------------------paramètres de miniaturisation
416  if ( sizeof( $images ) != 0 )
417  {
418    $output.= "
419        <style>
420                div.key
421                {
422                        margin-left : 10px;
423                }
424                td.choice
425                {
426                        text-align : center;
427                }
428        </style>";
429    $output.= "
430        <form method=\"post\" action=\"".add_session_id_to_url( "./admin.php?page=thumbnail&amp;dir=".$HTTP_GET_VARS['dir'] )."\">
431        <table style=\"width:100%;\">
432                <tr>
433                        <th colspan=\"3\">".$lang['tn_params_title']."</th>
434                </tr>";
435    $output.= "
436                <tr>
437                        <td colspan=\"3\">&nbsp;</td>
438                </tr>";
439    $output.= "
440                <tr>
441                        <td><div class=\"key\">".$lang['tn_params_GD']."</div></td>
442                        <td class=\"choice\">
443                                <input type=\"radio\" name=\"gd\" value=\"2\"/ checked=\"checked\">2.x
444                                <input type=\"radio\" name=\"gd\" value=\"1\"";
445    if ( $HTTP_POST_VARS['gd'] == 1 )
446    {
447      $output.= " checked=\"checked\"";
448    }
449    $output.= "/>1.x
450                        </td>
451                        <td style=\"width:50%;\" class=\"row2\">".$lang['tn_params_GD_info']."</td>
452                </tr>
453                <tr>
454                        <td><div class=\"key\">".$lang['tn_width']."</div></td>
455                        <td class=\"choice\"><input type=\"text\" name=\"width\" value=\"";
456    if ( isset( $HTTP_POST_VARS['width'] ) )
457    {
458      $output.= $HTTP_POST_VARS['width'];
459    }
460    else
461    {
462      $output.= "128";
463    }
464    $output.="\"/></td>
465                        <td class=\"row2\">".$lang['tn_params_width_info']."</td>
466                </tr>
467                <tr>
468                        <td><div class=\"key\">".$lang['tn_height']."</div></td>
469                        <td class=\"choice\"><input type=\"text\" name=\"height\" value=\"";
470    if ( isset( $HTTP_POST_VARS['height'] ) )
471    {
472      $output.= $HTTP_POST_VARS['height'];
473    }
474    else
475    {
476      $output.= "96";
477    }
478    $output.="\"/></td>
479                        <td class=\"row2\">".$lang['tn_params_height_info']."</td>
480                </tr>
481                <tr>
482                        <td><div class=\"key\">".$lang['tn_params_create']."</div></td>
483                        <td class=\"choice\">
484                                <select name=\"n\">
485                                        <option>5</option>
486                                        <option>10</option>
487                                        <option>20</option>
488                                        <option>40</option>
489                                </select>
490                        </td>
491                        <td class=\"row2\">".$lang['tn_params_create_info']."</td>
492                </tr>
493                <tr>
494                        <td><div class=\"key\">".$lang['tn_params_format']."</div></td>
495                        <td class=\"choice\"><span style=\"font-weight:bold;\">jpeg</span></td>
496                        <td class=\"row2\">".$lang['tn_params_format_info']."</td>
497                </tr>
498                <tr>
499                        <td colspan=\"3\">&nbsp;</td>
500                </tr>
501                <tr>
502                        <td colspan=\"3\" style=\"text-align:center;\">
503                                <input type=\"submit\" name=\"submit\" value=\"".$lang['submit']."\"/>
504                        </td>
505                </tr>";
506    $output.= "
507        </table>
508        </form>";
509    //-----------------------------------liste des images sans miniature
510    $images = get_images_without_thumbnail( $HTTP_GET_VARS['dir'] );
511    $style = "class=\"row2\" style=\"text-align:center;font-weight:bold;";
512    $output.= "
513        <table style=\"width:100%;\">
514                <tr>
515                        <th colspan=\"5\"><span style=\"color:#006699;\">".sizeof( $images )."</span> ".$lang['tn_alone_title']."</th>
516                </tr>
517                <tr>
518                        <td ".$style."\">&nbsp;</td>
519                        <td ".$style."width:50%;\">".$lang['tn_picture']."</td>
520                        <td ".$style."width:17%;\">".$lang['tn_filesize']."</td>
521                        <td ".$style."width:17%;\">".$lang['tn_width']."</td>
522                        <td ".$style."width:16%;\">".$lang['tn_height']."</td>
523                </tr>";
524    for ( $i = 0; $i < sizeof( $images ); $i++ )
525    {
526      $num = $i + 1;
527      $class = "";
528      if ( $i%2 == 1 )
529      {
530        $class = " class=\"row2\"";
531      }
532      $output.= "
533                <tr>
534                        <td class=\"row2\">".$num."</td>
535                        <td $class><div style=\"margin-left:10px;\">".$images[$i]['name']."</div></td>
536                        <td $class><div style=\"margin-left:10px;\">".$images[$i]['size']."</div></td>
537                        <td $class><div style=\"margin-left:10px;\">".$images[$i]['width']."</div></td>
538                        <td $class><div style=\"margin-left:10px;\">".$images[$i]['height']."</div></td>
539                </tr>";
540    }
541    $output.= "
542        </table>";
543  }
544}
545//-----------------------------------liste des répertoires
546//-------------------------si aucun répertoire selectionné
547else
548{
549  $output = "
550                <table style=\"width:100%;\">
551                        <tr>
552                                <th>".$lang['tn_dirs_title']."</th>
553                        </tr>";
554  $output.= "
555                        <tr>
556                                <td>
557                                        <div class=\"retrait\">
558                                                <img src=\"".$conf['lien_puce']."\" alt=\"\"/>galleries";
559  $output.= "<br />";
560  $output.= get_displayed_dirs( "../galleries", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" );
561  $output.= "
562                                        </div>
563                                </td>
564                        </tr>
565                </table>";
566}
567echo $output;
568?>
Note: See TracBrowser for help on using the repository browser.