Ignore:
Timestamp:
Apr 25, 2010, 2:36:35 AM (14 years ago)
Author:
patdenice
Message:

Square option is available for thumbnail creation page and upload form.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/square_thumbnails/thumbnail.php

    r5943 r5962  
    11<?php
    2 // +-----------------------------------------------------------------------+
    3 // | Piwigo - a PHP based picture gallery                                  |
    4 // +-----------------------------------------------------------------------+
    5 // | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
    6 // | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
    7 // | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
    8 // +-----------------------------------------------------------------------+
    9 // | This program is free software; you can redistribute it and/or modify  |
    10 // | it under the terms of the GNU General Public License as published by  |
    11 // | the Free Software Foundation                                          |
    12 // |                                                                       |
    13 // | This program is distributed in the hope that it will be useful, but   |
    14 // | WITHOUT ANY WARRANTY; without even the implied warranty of            |
    15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
    16 // | General Public License for more details.                              |
    17 // |                                                                       |
    18 // | You should have received a copy of the GNU General Public License     |
    19 // | along with this program; if not, write to the Free Software           |
    20 // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
    21 // | USA.                                                                  |
    22 // +-----------------------------------------------------------------------+
    232
    24 include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    254
    26 // +-----------------------------------------------------------------------+
    27 // | Check Access and exit when user status is not ok                      |
    28 // +-----------------------------------------------------------------------+
    29 check_status(ACCESS_ADMINISTRATOR);
    30 
    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($path, $newWidth, $newHeight, $tn_ext)
    36 {
    37   global $conf, $lang, $page;
    38 
    39   if (!function_exists('gd_info'))
    40   {
    41     return;
    42   }
    43 
    44   $filename = basename($path);
    45   $dirname = dirname($path);
    46  
    47   // extension of the picture filename
    48   $extension = get_extension($filename);
    49 
    50   if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
    51   {
    52     $srcImage = @imagecreatefromjpeg($path);
    53   }
    54   else if ($extension == 'png' or $extension == 'PNG')
    55   {
    56     $srcImage = @imagecreatefrompng($path);
    57   }
    58   else
    59   {
    60     unset($extension);
    61   }
    62 
    63   if ( isset( $srcImage ) )
    64   {
    65     // width/height
    66     $srcWidth    = imagesx( $srcImage );
    67     $srcHeight   = imagesy( $srcImage );
    68     $x = 0;
    69     $y = 0;
    70 
    71     if (isset($_POST['square']))
    72     {
    73       if($srcWidth > $srcHeight)
    74       {
    75         $x = ceil(($srcWidth - $srcHeight) / 2 );
    76         $srcWidth = $srcHeight;
    77       }
    78       elseif ($srcHeight > $srcWidth)
    79       {
    80         $y = ceil(($srcHeight - $srcWidth) / 2);
    81         $srcHeight = $srcWidth;
    82       }
    83     }
    84 
    85     $ratioWidth  = $srcWidth/$newWidth;
    86     $ratioHeight = $srcHeight/$newHeight;
    87 
    88     // maximal size exceeded ?
    89     if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
    90     {
    91       if ( $ratioWidth < $ratioHeight)
    92       {
    93         $destWidth = $srcWidth/$ratioHeight;
    94         $destHeight = $newHeight;
    95       }
    96       else
    97       {
    98         $destWidth = $newWidth;
    99         $destHeight = $srcHeight/$ratioWidth;
    100       }
    101     }
    102     else
    103     {
    104       $destWidth = $srcWidth;
    105       $destHeight = $srcHeight;
    106     }
    107 
    108     // according to the GD version installed on the server
    109     if ( $_POST['gd'] == 2 )
    110     {
    111       // GD 2.0 or more recent -> good results (but slower)
    112       $destImage = imagecreatetruecolor( $destWidth, $destHeight);
    113       imagecopyresampled( $destImage, $srcImage, 0, 0, $x, $y,
    114                           $destWidth,$destHeight,$srcWidth,$srcHeight );
    115     }
    116     else
    117     {
    118       // GD prior to version  2 -> pretty bad results :-/ (but fast)
    119       $destImage = imagecreate( $destWidth, $destHeight);
    120       imagecopyresized( $destImage, $srcImage, 0, 0, $x, $y,
    121                         $destWidth,$destHeight,$srcWidth,$srcHeight );
    122     }
    123 
    124     if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false)
    125     {
    126       return false;
    127     }
    128 
    129     $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
    130     $dest_file.= get_filename_wo_extension($filename);
    131     $dest_file.= '.'.$tn_ext;
    132    
    133     // creation and backup of final picture
    134     if (!is_writable($tndir))
    135     {
    136       array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access'));
    137       return false;
    138     }
    139     imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
    140     // freeing memory ressources
    141     imagedestroy( $srcImage );
    142     imagedestroy( $destImage );
    143    
    144     list($tn_width, $tn_height) = getimagesize($dest_file);
    145     $tn_size = floor(filesize($dest_file) / 1024).' KB';
    146    
    147     $info = array( 'path'      => $path,
    148                    'tn_file'   => $dest_file,
    149                    'tn_width'  => $tn_width,
    150                    'tn_height' => $tn_height,
    151                    'tn_size'   => $tn_size );
    152     return $info;
    153   }
    154   // error
    155   else
    156   {
    157     echo l10n('Picture unreachable or no support')." ";
    158     if ( isset( $extenstion ) )
    159     {
    160       echo l10n('for the file format').' '.$extension;
    161     }
    162     else
    163     {
    164       echo l10n('for this file format');
    165     }
    166     exit();
    167   }
    168 }
    169 
    170 $pictures = array();
    171 $stats = array();
    172 
    173 if (!function_exists('gd_info'))
    174 {
    175   array_push($page['errors'], l10n('GD library is missing'));
    176 }
    177 
    178 // +-----------------------------------------------------------------------+
    179 // |                       template initialization                         |
    180 // +-----------------------------------------------------------------------+
    181 $template->set_filename('thumbnail', realpath(SQUARE_THUMB_PATH.'thumbnail.tpl'));
     5global $template;
    1826
    1837load_language('plugin.lang', SQUARE_THUMB_PATH);
    1848
    185 $template->assign(
    186   array('U_HELP' => get_root_url().'admin/popuphelp.php?page=thumbnail')
    187   );
    188 // +-----------------------------------------------------------------------+
    189 // |                   search pictures without thumbnails                  |
    190 // +-----------------------------------------------------------------------+
    191 $wo_thumbnails = array();
    192 $thumbnalized = array();
     9// Add handler for square resize
     10if (isset($_POST['square']))
     11{
     12  include(SQUARE_THUMB_PATH.'functions.inc.php');
     13}
    19314
    194 // what is the directory to search in ?
    195 $query = '
    196 SELECT galleries_url FROM '.SITES_TABLE.'
    197   WHERE galleries_url NOT LIKE "http://%"
    198 ;';
    199 $result = pwg_query($query);
    200 while ( $row=pwg_db_fetch_assoc($result) )
     15// Add prefilter
     16$template->set_prefilter('thumbnail', 'add_square_option');
     17
     18function add_square_option($content, $smarty)
    20119{
    202   $basedir = preg_replace('#/*$#', '', $row['galleries_url']);
    203   $fs = get_fs($basedir);
     20  $search = '<li>
     21        <span class="property">
     22          <label for="width">{\'maximum width\'|@translate}</label>';
    20423
    205   // because isset is one hundred time faster than in_array
    206   $fs['thumbnails'] = array_flip($fs['thumbnails']);
    207 
    208   foreach ($fs['elements'] as $path)
    209   {
    210     // only pictures need thumbnails
    211     if (in_array(get_extension($path), $conf['picture_ext']))
    212     {
    213       $dirname = dirname($path);
    214       $filename = basename($path);
    215  
    216       // only files matching the authorized filename pattern can be considered
    217       // as "without thumbnail"
    218       if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
    219       {
    220         continue;
    221       }
    222      
    223       // searching the element
    224       $filename_wo_ext = get_filename_wo_extension($filename);
    225       $tn_ext = '';
    226       $base_test = $dirname.'/'.$conf['dir_thumbnail'].'/';
    227       $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
    228       foreach ($conf['picture_ext'] as $ext)
    229       {
    230         if (isset($fs['thumbnails'][$base_test.$ext]))
    231         {
    232           $tn_ext = $ext;
    233           break;
    234         }
    235       }
    236      
    237       if (empty($tn_ext))
    238       {
    239         array_push($wo_thumbnails, $path);
     24  $replacement = '
     25{html_head}
     26{literal}
     27<script type="text/javascript">
     28jQuery().ready(function(){
     29  jQuery(":text").keyup(function(){
     30    if(jQuery("#square").attr("checked")){
     31      if (this.id == "width"){
     32        jQuery("#height").attr("value", this.value);
     33      }else{
     34        jQuery("#width").attr("value", this.value);
    24035      }
    24136    }
    242   } // next element
    243 } // next site id
    244 // +-----------------------------------------------------------------------+
    245 // |                         thumbnails creation                           |
    246 // +-----------------------------------------------------------------------+
    247 if (isset($_POST['submit']))
    248 {
    249   $times = array();
    250   $infos = array();
    251  
    252   // checking criteria
    253   if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10)
    254   {
    255     array_push($page['errors'], l10n('width must be a number superior to').' 10');
    256   }
    257   if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10)
    258   {
    259     array_push($page['errors'], l10n('height must be a number superior to').' 10');
    260   }
    261  
    262   // picture miniaturization
    263   if (count($page['errors']) == 0)
    264   {
    265     $num = 1;
    266     foreach ($wo_thumbnails as $path)
    267     {
    268       if (is_numeric($_POST['n']) and $num > $_POST['n'])
    269       {
    270         break;
    271       }
    272      
    273       $starttime = get_moment();
    274       if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg'))
    275       {
    276         $endtime = get_moment();
    277         $info['time'] = ($endtime - $starttime) * 1000;
    278         array_push($infos, $info);
    279         array_push($times, $info['time']);
    280         array_push($thumbnalized, $path);
    281         $num++;
    282       }
    283       else
    284       {
    285         break;
    286       }
    287     }
     37  });
     38  jQuery("#square").click(function(){
     39    if (this.checked)
     40      jQuery("#height").attr("value", jQuery("#width").attr("value"));
     41  });
     42});
     43</script>
     44{/literal}
     45{/html_head}
    28846
    289     if (count($infos) > 0)
    290     {
    291       $sum = array_sum($times);
    292       $average = $sum / count($times);
    293       sort($times, SORT_NUMERIC);
    294       $max = array_pop($times);
    295       if (count($thumbnalized) == 1)
    296       {
    297         $min = $max;
    298       }
    299       else
    300       {
    301         $min = array_shift($times);
    302       }
    303      
    304       $tpl_var =
    305         array(
    306           'TN_NB'=>count($infos),
    307           'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms',
    308           'TN_MAX'=>number_format($max, 2, '.', ' ').' ms',
    309           'TN_MIN'=>number_format($min, 2, '.', ' ').' ms',
    310           'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms',
    311           'elements' => array()
    312           );
    313      
    314       foreach ($infos as $i => $info)
    315       {
    316         $tpl_var['elements'][] =
    317           array(
    318             'PATH'=>$info['path'],
    319             'TN_FILE_IMG'=>$info['tn_file'],
    320             'TN_FILESIZE_IMG'=>$info['tn_size'],
    321             'TN_WIDTH_IMG'=>$info['tn_width'],
    322             'TN_HEIGHT_IMG'=>$info['tn_height'],
    323             'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms',
    324             );
    325       }
    326       $template->assign('results', $tpl_var);
    327     }
    328   }
    329 }
    330 // +-----------------------------------------------------------------------+
    331 // |             form & pictures without thumbnails display                |
    332 // +-----------------------------------------------------------------------+
    333 $remainings = array_diff($wo_thumbnails, $thumbnalized);
     47      <li>
     48        <span class="property">{\'Square Thumbnails\'|@translate}</span>
     49             <label><input type="checkbox" name="square" id="square" {if $params.SQUARE}checked="checked"{/if}></label>
     50      </li>
    33451
    335 if (count($remainings) > 0)
    336 {
    337   $form_url = get_admin_plugin_menu_link(SQUARE_THUMB_PATH.'thumbnail.php');
    338   $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2;
    339   $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width'];
    340   $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height'];
    341   $n = !empty($_POST['n']) ? $_POST['n'] : 5;
    342  
    343   $template->assign(
    344     'params',
    345     array(
    346       'F_ACTION'=> $form_url,
    347       'GD_SELECTED' => $gd,
    348       'SQUARE' => isset($_POST['square']),
    349       'N_SELECTED' => $n,
    350       'WIDTH_TN'=>$width,
    351       'HEIGHT_TN'=>$height
    352       ));
     52      <li>
     53        <span class="property">
     54          <label for="width">{\'maximum width\'|@translate}</label>';
    35355
    354   $template->assign(
    355     'TOTAL_NB_REMAINING',
    356     count($remainings));
    357 
    358   foreach ($remainings as $path)
    359   {
    360     list($width, $height) = getimagesize($path);
    361     $size = floor(filesize($path) / 1024).' KB';
    362 
    363     $template->append(
    364       'remainings',
    365       array(
    366         'PATH'=>$path,
    367         'FILESIZE_IMG'=>$size,
    368         'WIDTH_IMG'=>$width,
    369         'HEIGHT_IMG'=>$height,
    370         ));
    371   }
     56  return str_replace($search, $replacement, $content);
    37257}
    37358
    374 // +-----------------------------------------------------------------------+
    375 // |                           return to admin                             |
    376 // +-----------------------------------------------------------------------+
    377 $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail');
    37859?>
Note: See TracChangeset for help on using the changeset viewer.