b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/watermark_plugin/_watermark.plugin.php - 296 lines - 8009 bytes - Summary - Text - Print

Description: This file implements the Watermark plugin for b2evolution b2evolution - {@link http://b2evolution.net/} Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   1  <?php
   2  /**
   3   * This file implements the Watermark plugin for b2evolution
   4   *
   5   * b2evolution - {@link http://b2evolution.net/}
   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   7   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   8   *
   9   * @package plugins
  10   *
  11   * @author sam2kb: Alex - {@link http://b2evo.sonorth.com/}
  12   */
  13  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  14  
  15  /**
  16   * Adds text watermark to generated thumbnails
  17   *
  18   * @package plugins
  19   */
  20  class watermark_plugin extends Plugin
  21  {
  22      var $code = 'evo_watermark';
  23      var $name = 'Watermark';
  24      var $group = 'files';
  25      var $priority = 10;
  26      var $short_desc;
  27      var $long_desc;
  28      var $version = '5.0.0';
  29      var $number_of_installs = 1;
  30  
  31      var $fonts_dir = '';
  32  
  33      /**
  34       * Init
  35       */
  36  	function PluginInit( & $params )
  37      {
  38          $this->short_desc = T_('Adds text watermark to generated thumbnails');
  39          $this->long_desc = T_('Adds text watermark to generated thumbnails greater than 160x160 pixels');
  40  
  41          $this->fonts_dir = dirname(__FILE__).'/fonts';
  42      }
  43  
  44  
  45      /**
  46       * Get the settings that the plugin can use.
  47       *
  48       * @return array
  49       */
  50  	function GetDefaultSettings( & $params )
  51      {
  52          global $ReqHost;
  53  
  54          return array(
  55              'text' => array(
  56                      'label' => 'Image text',
  57                      'size' => 70,
  58                      'defaultvalue' => '&copy; '.$ReqHost,
  59                      'note' => T_('The text to write on the image.'),
  60                  ),
  61              'font' => array(
  62                      'label' => 'Font file name',
  63                      'size' => 30,
  64                      'defaultvalue' => '',
  65                      'note' => sprintf(T_('You can upload your own fonts to the plugin\'s font directory (%s), then use the filename here. By default "%s" is used.'),
  66                          rel_path_to_base($this->fonts_dir), rel_path_to_base($this->get_default_font())),
  67                  ),
  68              'font_size' => array(
  69                      'label' => 'Font size',
  70                      'type' => 'select',
  71                      'options' => array(6=>6,8=>8,10=>10,12=>12,14=>14,16=>16,18=>18,20=>20,22=>22,24=>24,
  72                              26=>26,28=>28,30=>30,32=>32,34=>34,36=>36,38=>38,40=>40,42=>42,44=>44,46=>46,
  73                              48=>48,50=>50,52=>52,54=>54,56=>56,58=>58,60=>60,62=>62,64=>64,66=>66,68=>68),
  74                      'defaultvalue' => 12,
  75                      'note' => '',
  76                  ),
  77              );
  78      }
  79  
  80  
  81      /**
  82       * Define here default collection/blog settings that are to be made available in the backoffice.
  83       *
  84       * @return array See {@link Plugin::GetDefaultSettings()}.
  85       */
  86  	function get_coll_setting_definitions( & $params )
  87      {
  88          $r = array(
  89              'coll_text' => array(
  90                      'label' => 'Image text',
  91                      'size' => 70,
  92                      'defaultvalue' => $this->Settings->get('text'),
  93                      'note' => T_('The text to write on the image.'),
  94                  ),
  95              'coll_font' => array(
  96                      'label' => 'Font file name',
  97                      'size' => 30,
  98                      'defaultvalue' => $this->Settings->get('font'),
  99                      'note' => sprintf(T_('You can upload your own fonts to the plugin\'s font directory (%s), then use the filename here. By default "%s" is used.'),
 100                          rel_path_to_base($this->fonts_dir), rel_path_to_base($this->get_default_font())),
 101                  ),
 102              'coll_font_size' => array(
 103                      'label' => 'Font size',
 104                      'type' => 'select',
 105                      'options' => array(6=>6,8=>8,10=>10,12=>12,14=>14,16=>16,18=>18,20=>20,22=>22,24=>24,
 106                              26=>26,28=>28,30=>30,32=>32,34=>34,36=>36,38=>38,40=>40,42=>42,44=>44,46=>46,
 107                              48=>48,50=>50,52=>52,54=>54,56=>56,58=>58,60=>60,62=>62,64=>64,66=>66,68=>68),
 108                      'defaultvalue' => $this->Settings->get('font_size'),
 109                      'note' => '',
 110                  ),
 111              );
 112  
 113          return $r;
 114      }
 115  
 116  
 117  	function PluginSettingsUpdateAction()
 118      {
 119          $font = $this->Settings->get('font');
 120  
 121          return $this->settings_update_action( $font );
 122      }
 123  
 124  
 125  	function PluginCollSettingsUpdateAction()
 126      {
 127          global $Blog;
 128  
 129          $font = $this->get_coll_setting('coll_font', $Blog);
 130  
 131          return $this->settings_update_action( $font );
 132      }
 133  
 134  
 135  	function BeforeEnable()
 136      {
 137          if( !function_exists('imagettftext') )
 138          {    // The function imagettftext() is not available.
 139              return 'The function imagettftext() is not available.'; // Conf error, no translation
 140          }
 141  
 142          delete_cachefolders();
 143  
 144          return true;
 145      }
 146  
 147  
 148  	function BeforeDisable()
 149      {
 150          delete_cachefolders();
 151      }
 152  
 153  
 154      /**
 155       * Get hardcoded default font.
 156       * @return string
 157       */
 158  	function get_default_font()
 159      {
 160          global $rsc_path;
 161          return $rsc_path.'fonts/LiberationSans-Regular.ttf';
 162      }
 163  
 164  
 165  	function settings_update_action( $font = '' )
 166      {
 167          if( !empty($font) && !is_readable($this->fonts_dir.'/'.$font) )
 168          {
 169              $this->msg( sprintf( T_('Unable to load font file: %s'), $this->fonts_dir.'/'.$font ), 'error' );
 170              return false;
 171          }
 172  
 173          // Delete file cache
 174          // TODO> clear cache only if settings have changed
 175          //       (could use PluginSettingsValidateSet for this)
 176          delete_cachefolders();
 177      }
 178  
 179  
 180      /**
 181       * This gets called before an image thumbnail gets created.
 182       *
 183       * This is useful to post-process the thumbnail image (add a watermark or change colors).
 184       *
 185       * @param array Associative array of parameters
 186       *   - 'File': the related File (by reference)
 187       *   - 'imh': image resource (by reference)
 188       *   - 'size': size name (by reference)
 189       *   - 'mimetype': mimetype of thumbnail (by reference)
 190       *   - 'quality': JPEG image quality [0-100] (by reference)
 191       *   - 'root_type': file root type 'user', 'group', 'collection' etc. (by reference)
 192       *   - 'root_type_ID': ID of user, group or collection (by reference)
 193       */
 194  	function BeforeThumbCreate( & $params )
 195      {
 196          if( $params['root_type'] == 'collection' )
 197          {
 198              $BlogCache = & get_BlogCache();
 199              $Blog = & $BlogCache->get_by_ID( $params['root_type_ID'], false, false );
 200          }
 201  
 202          if( !empty($Blog) )
 203          {    // Use blog settings when possible
 204              $font = $this->get_coll_setting('coll_font', $Blog);
 205              $text = $this->get_coll_setting('coll_text', $Blog);
 206              $font_size = $this->get_coll_setting('coll_font_size', $Blog);
 207          }
 208          else
 209          {    // Use global settings
 210              $font = $this->Settings->get('font');
 211              $text = $this->Settings->get('text');
 212              $font_size = $this->Settings->get('font_size');
 213          }
 214          
 215          if( !function_exists('imagettftext') )
 216          {    // The function imagettftext() is not available.
 217              return;
 218          }
 219  
 220          // Canvas width & height
 221          $width = imagesx( $params['imh'] ) ;
 222          $height = imagesy( $params['imh'] );
 223  
 224          if( $width < 161 || $height < 161 )
 225          {    // Skip small thumbnails
 226              return;
 227          }
 228  
 229          if( $font )
 230          {    // Custom font
 231              $font = $this->fonts_dir.'/'.$font;
 232          }
 233          else
 234          {
 235              $font = $this->get_default_font();
 236          }
 237  
 238          if( !is_readable($font) )
 239          {    // Font file not found
 240              $this->debug_log( sprintf('Font file (%s) is not readable!', $font) );
 241              return;
 242          }
 243  
 244          $text = html_entity_decode( $text );
 245  
 246          // Text margins
 247          $margin_left = 10;
 248          $margin_bottom = 10;
 249  
 250          // Create a transparent image
 251          $im = imagecreatetruecolor($width, $height);
 252          imagealphablending($im, true);
 253          imagefill($im, 0, 0, imagecolortransparent($im, imagecolorallocatealpha($im, 0, 0, 0, 127)));
 254          imagesavealpha($im, true);
 255  
 256          // Create some colors
 257          $white = imagecolorallocate($im, 255, 255, 255);
 258          $light_grey = imagecolorallocate($im, 230, 230, 230);
 259          $grey = imagecolorallocate($im, 60, 60, 60);
 260          $black = imagecolorallocate($im, 0, 0, 0);
 261  
 262          // Add text shadow
 263          imagettftext($im, $font_size, 0, $margin_left + 2, $height - $margin_bottom, $grey, $font, $text);
 264  
 265          // Add text
 266          imagettftext($im, $font_size, 0, $margin_left, $height - $margin_bottom, $light_grey, $font, $text);
 267  
 268          // Merge images
 269          imagecopyresampled($params['imh'], $im, 0, 0, 0, 0, $width, $height, $width, $height);
 270  
 271          // Low quality :(
 272          //imagecopymerge($params['imh'], $im, 0, 0, 0, 0, $width, $height, 90);
 273  
 274          return true;
 275      }
 276  
 277  
 278      /**
 279       * Event handler: Called before an uploaded file gets saved on server.
 280       *
 281       * @param array Associative array of parameters
 282       *   - 'File': The "File" object (by reference).
 283       *   - 'name': file name (by reference).
 284       *   - 'type': file mimetype (by reference).
 285       *   - 'tmp_name': file location (by reference).
 286       *   - 'size': file size in bytes  (by reference).
 287       *
 288       * @return boolean 'false' to abort file upload, otherwise return 'true'
 289       */
 290  	function AfterFileUpload( & $params )
 291      {
 292          return true;
 293      }
 294  }
 295  
 296  ?>

title

Description

title

Description

title

Description

title

title

Body