[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezimage/classes/ -> ezimagehandler.php (source)

   1  <?php
   2  //
   3  // Definition of eZImageHandler class
   4  //
   5  // Created on: <16-Oct-2003 13:58:34 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezimagehandler.php
  30  */
  31  
  32  /*!
  33    \class eZImageHandler ezimagehandler.php
  34    \ingroup eZImage
  35    \brief The class eZImageHandler does
  36  
  37  */
  38  
  39  define( "EZ_IMAGE_HANDLER_KEEP_SUFFIX", 1 );
  40  define( "EZ_IMAGE_HANDLER_REPLACE_SUFFIX", 2 );
  41  define( "EZ_IMAGE_HANDLER_PREPEND_TAG_REPLACE_SUFFIX", 3 );
  42  
  43  class eZImageHandler
  44  {
  45      /*!
  46       Initializes the image handler with data sent from the inheriting class.
  47       \param $handlerName The name of the current handler
  48       \param $isEnabled A boolean which tells whether the handler can be used or not
  49       \param $outputRewriteType Defines how output filenames are rewritten
  50       \param $supportedInputMIMETypes A list of MIME-Types the handler supports as input or \c false if no type as defined
  51       \param $supportedOutputMIMETypes A list of MIME-Types the handler supports as output or \c false if no type as defined
  52       \param $conversionRules A list of conversion rules specific for this handler, is combined with the global rules
  53       \param $filters A list of filters this handler supports
  54       \param $mimeTagMap A mapping table which maps from a MIME-Type to a specific tag, this tag can be used when rewriting the filename.
  55      */
  56      function eZImageHandler( $handlerName, $isEnabled = true, $outputRewriteType = EZ_IMAGE_HANDLER_REPLACE_SUFFIX,
  57                               $supportedInputMIMETypes = false, $supportedOutputMIMETypes,
  58                               $conversionRules = false, $filters = false, $mimeTagMap = false )
  59      {
  60          $this->HandlerName = $handlerName;
  61          $this->SupportedInputMIMETypes = $supportedInputMIMETypes;
  62          $this->SupportedOutputMIMETypes = $supportedOutputMIMETypes;
  63          $this->ConversionRules = $conversionRules;
  64          $this->OutputRewriteType = $outputRewriteType;
  65          $this->Filters = $filters;
  66          $this->FilterMap = array();
  67          $this->SupportImageFilters = array();
  68          $this->MIMETagMap = array();
  69          if ( $mimeTagMap )
  70              $this->MIMETagMap = $mimeTagMap;
  71          if ( $this->Filters)
  72          {
  73              foreach ( array_keys( $this->Filters ) as $filterKey )
  74              {
  75                  $filter =& $this->Filters[$filterKey];
  76                  $this->FilterMap[$filter['name']] =& $filter;
  77                  $this->SupportImageFilters[] = $filter['name'];
  78              }
  79          }
  80          $this->SupportImageFilters = array_unique( $this->SupportImageFilters );
  81          $this->IsEnabled = $isEnabled;
  82      }
  83  
  84      /*!
  85       \virtual
  86       \return whether this handler can be used or not.
  87  
  88       Implementors of image handlers should implement this to return true if
  89       the image conversion system to be used is available, for instance to check
  90       for a PHP extension.
  91       \note default is to return \c true.
  92      */
  93      function isAvailable()
  94      {
  95          return $this->IsEnabled;
  96      }
  97  
  98      /*!
  99       \virtual
 100       \return the tag for the MIME type named \a $mimeName.
 101       This is a helper function for some shell based handlers, it will create a
 102       proper name from the MIME type \a $mimeData.
 103       \note The default returns the type part of the MIME type.
 104      */
 105      function tagForMIMEType( $mimeData )
 106      {
 107          $name = $mimeData['name'];
 108          if ( isset( $this->MIMETagMap[$name] ) )
 109              return $this->MIMETagMap[$name];
 110          $position = strpos( $name, '/' );
 111          if ( $position !== false )
 112              return substr( $name, $position + 1 );
 113          else
 114              return false;
 115      }
 116  
 117      /*!
 118       \return an array with the names of the filters this handler can work with.
 119      */
 120      function supportedImageFilters()
 121      {
 122          return $this->SupportImageFilters;
 123      }
 124  
 125      /*!
 126       \return The conversion rules for this handler.
 127      */
 128      function conversionRules()
 129      {
 130          return $this->ConversionRules;
 131       }
 132  
 133      /*!
 134       Parses the filter text \a $filterText which is taken from an INI file
 135       and returns a filter definition structure for it.
 136      */
 137      function createFilterDefinitionFromINI( $filterText )
 138      {
 139          $equalPosition = strpos( $filterText, '=' );
 140          $filterData = false;
 141          if ( $equalPosition !== false )
 142          {
 143              $filterName = substr( $filterText, 0, $equalPosition );
 144              $filterData = substr( $filterText, $equalPosition + 1 );
 145          }
 146          else
 147              $filterName = $filterText;
 148          return array( 'name' => $filterName,
 149                        'text' => $filterData );
 150      }
 151  
 152      /*!
 153       Converts a filter definition and filter data into a text string.
 154       This string is usually the commandline parameter.
 155      */
 156      function convertFilterToText( $filterDefinition, $filterData )
 157      {
 158          $replaceList = array();
 159          if ( $filterData['data'] )
 160          {
 161              $counter = 1;
 162              foreach ( $filterData['data'] as $data )
 163              {
 164                  $replaceList['%' . $counter] = $data;
 165                  ++$counter;
 166              }
 167          }
 168          $argumentText = $filterDefinition['text'];
 169          $text = eZSys::createShellArgument( $argumentText, $replaceList );
 170          return $text;
 171      }
 172  
 173      /*!
 174       Calls convertFilterToText with the correct filter definition and returns the text.
 175      */
 176      function textForFilter( $filterData )
 177      {
 178          $text = false;
 179          if ( isset( $this->FilterMap[$filterData['name']] ) )
 180          {
 181              $filterDefinition =& $this->FilterMap[$filterData['name']];
 182              $text = $this->convertFilterToText( $filterDefinition, $filterData );
 183          }
 184          return $text;
 185      }
 186  
 187      /*!
 188       \virtual
 189       Rewrites the URL in \a $originalMimeData to become a url for \a $destinationMimeData.
 190       The type of rewrite is determined by \a $rewriteType which can be one of:
 191       - EZ_IMAGE_HANDLER_KEEP_SUFFIX - Does nothing to the url
 192       - EZ_IMAGE_HANDLER_REPLACE_SUFFIX - Replaces the suffix or the url
 193       - EZ_IMAGE_HANDLER_PREPEND_TAG_REPLACE_SUFFIX - Prepends the tag name and replaces the suffix of the url
 194       The new url is placed in the \a $destinationMimeData.
 195      */
 196      function rewriteURL( $originalMimeData, &$destinationMimeData, $rewriteType, $aliasName = false )
 197      {
 198          $extraText = false;
 199          if ( $aliasName and
 200               $aliasName != 'original' )
 201              $extraText = '_' . $aliasName;
 202          switch ( $rewriteType )
 203          {
 204              case EZ_IMAGE_HANDLER_KEEP_SUFFIX:
 205              {
 206                  $destinationMimeData['basename'] = $originalMimeData['basename'];
 207                  $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $originalMimeData['suffix'];
 208                  $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
 209                  if ( $originalMimeData['dirpath'] )
 210                      $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
 211                  else
 212                      $destinationMimeData['url'] = $destinationMimeData['filename'];
 213              } break;
 214              case EZ_IMAGE_HANDLER_REPLACE_SUFFIX:
 215              {
 216                  $destinationMimeData['basename'] = $originalMimeData['basename'];
 217                  $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
 218                  $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
 219                  if ( $originalMimeData['dirpath'] )
 220                      $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
 221                  else
 222                      $destinationMimeData['url'] = $destinationMimeData['filename'];
 223              } break;
 224              case EZ_IMAGE_HANDLER_PREPEND_TAG_REPLACE_SUFFIX:
 225              {
 226                  $tagName = $this->tagForMIMEType( $destinationMimeData );
 227                  $destinationMimeData['basename'] = $originalMimeData['basename'];
 228                  $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
 229                  $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
 230                  if ( $originalMimeData['dirpath'] )
 231                      $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
 232                  else
 233                      $destinationMimeData['url'] = $destinationMimeData['filename'];
 234                  $destinationMimeData['url'] = $tagName . ':' . $destinationMimeData['url'];
 235              } break;
 236          }
 237      }
 238  
 239      /*!
 240       \virtual
 241       \return an array with MIME type names that the handler supports as input.
 242               MIME type names can also be specified with wildcards, for instance
 243               image/* to say that all image types are supported.
 244       \note The default implementation returns the MIME types specified in the constructor
 245      */
 246      function supportedInputMIMETypes()
 247      {
 248          return $this->SupportedInputMIMETypes;
 249      }
 250  
 251      /*!
 252       \virtual
 253       \return an array with MIME type names that the handler supports as output.
 254               MIME type names can also be specified with wildcards, for instance
 255               image/* to say that all image types are supported.
 256       \note The default implementation returns the MIME types specified in the constructor
 257      */
 258      function supportedOutputMIMETypes()
 259      {
 260          return $this->SupportedOutputMIMETypes;
 261      }
 262  
 263      /*!
 264       \static
 265       Changes the file permissions for image file \a $filepath to the ones
 266       defines in image.ini. It uses the group FileSettings and variable ImagePermissions.
 267       \return \c true on success, \c false otherwise
 268      */
 269      function changeFilePermissions( $filepath )
 270      {
 271          if ( !file_exists( $filepath ) )
 272              return false;
 273          $ini =& eZINI::instance( 'image.ini' );
 274          $perm = $ini->variable( "FileSettings", "ImagePermissions" );
 275          $success = false;
 276          $oldmask = umask( 0 );
 277          if ( !chmod( $filepath, octdec( $perm ) ) )
 278              eZDebug::writeError( "Chmod $perm $filepath failed",
 279                                   'eZImageHandler::changeFilePermissions' );
 280          else
 281              $success = true;
 282          umask( $oldmask );
 283          return $success;
 284      }
 285  
 286      /*!
 287       \static
 288       Creats a regexp string out of the wildcard \a $wilcard and returns it.
 289      */
 290      function wildcardToRegexp( $wildcard, $separatorCharacter = false )
 291      {
 292          if ( !$separatorCharacter )
 293              $separatorCharacter = '#';
 294          $wildcardArray = preg_split( "#[*]#", $wildcard, -1, PREG_SPLIT_DELIM_CAPTURE );
 295          $wildcardList = array();
 296          $i = 0;
 297          foreach ( $wildcardArray as $wildcardElement )
 298          {
 299              if ( ( $i % 2 ) == 1 )
 300                  $wildcardList[] = '(.+)';
 301              else
 302                  $wildcardList[] = preg_quote( $wildcardElement, $separatorCharacter );
 303              ++$i;
 304          }
 305          $wildcardString = implode( '', $wildcardList );
 306          return $wildcardString;
 307      }
 308  
 309      /*!
 310       \return \c true if the MIME-Type defined in \a $mimeData is supported as output by this handler.
 311      */
 312      function isOutputMIMETypeSupported( $mimeData )
 313      {
 314          $mimeTypes = $this->supportedOutputMIMETypes();
 315          $mimeName = $mimeData;
 316          if ( is_array( $mimeData ) )
 317              $mimeName = $mimeData['name'];
 318          foreach ( $mimeTypes as $mimeType )
 319          {
 320              if ( strpos( $mimeType, '*' ) !== false )
 321              {
 322                  $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
 323                  if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
 324                  {
 325                      return true;
 326                  }
 327              }
 328              else if ( $mimeType == $mimeName )
 329              {
 330                  return true;
 331              }
 332          }
 333          return false;
 334      }
 335  
 336      /*!
 337       \return \c true if the MIME-Type defined in \a $mimeData is supported as input by this handler.
 338      */
 339      function isInputMIMETypeSupported( $mimeData )
 340      {
 341          $mimeTypes = $this->supportedInputMIMETypes();
 342          $mimeName = $mimeData;
 343          if ( is_array( $mimeData ) )
 344              $mimeName = $mimeData['name'];
 345          foreach ( $mimeTypes as $mimeType )
 346          {
 347              if ( strpos( $mimeType, '*' ) !== false )
 348              {
 349                  $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
 350                  if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
 351                  {
 352                      return true;
 353                  }
 354              }
 355              else if ( $mimeType == $mimeName )
 356              {
 357                  return true;
 358              }
 359          }
 360          return false;
 361      }
 362  
 363      /*!
 364       \virtual
 365       Figures out the output MIME type for the \a $currentMimeData. It goes trough
 366       all conversion rules for this handler and returns a MIME structure for the
 367       possible output. The returned structure also contains the correct url for the output.
 368       \param $wantedMimeData an optional MIME structure for the wanted output type,
 369                              if a direct conversion rule exists from \a $currentMimeData to \a $wantedMimeData
 370                              then this is used.
 371       \param $aliasName An optional name for the current alias being used, if supplied
 372                         the output MIME structure will have the alias name in the filename.
 373      */
 374      function outputMIMEType( &$manager, $currentMimeData, $wantedMimeData, $supportedFormatsOriginal, $aliasName = false )
 375      {
 376          $conversionRules = array_merge( $manager->conversionRules(), $this->conversionRules() );
 377          $mimeData = false;
 378          $mimeType = false;
 379          if ( !$this->isInputMIMETypeSupported( $currentMimeData ) )
 380              return false;
 381  
 382          if ( $wantedMimeData )
 383          {
 384              $conversionRules = array_merge( array( array( 'from' => $currentMimeData['name'],
 385                                                            'to' => $wantedMimeData['name'] ) ),
 386                                              $conversionRules );
 387          }
 388  
 389          $supportedFormats = array();
 390          foreach ( $supportedFormatsOriginal as $supportedFormat )
 391          {
 392              if ( $this->isOutputMIMETypeSupported( $supportedFormat ) )
 393              {
 394                  $supportedFormats[] = $supportedFormat;
 395                  $conversionRules[] = array( 'from' => $supportedFormat,
 396                                              'to' => $supportedFormat );
 397              }
 398          }
 399  
 400          if ( $wantedMimeData and
 401               in_array( $wantedMimeData['name'], $supportedFormats ) )
 402          {
 403              $mimeType = $wantedMimeData['name'];
 404          }
 405          else if ( is_array( $conversionRules ) )
 406          {
 407              foreach ( $conversionRules as $rule )
 408              {
 409                  if ( !$this->isOutputMIMETypeSupported( $rule['to'] ) or
 410                       !in_array( $rule['to'], $supportedFormats ) )
 411                      continue;
 412  
 413                  $matchRule = false;
 414                  if ( strpos( $rule['from'], '*' ) !== false )
 415                  {
 416                      $matchString = eZImageHandler::wildcardToRegexp( $rule['from'] );
 417                      if ( preg_match( "#^" . $matchString . "$#", $currentMimeData['name'] ) )
 418                      {
 419                          $matchRule = $rule;
 420                      }
 421                  }
 422                  else if ( $rule['from'] == $currentMimeData['name'] )
 423                  {
 424                      $matchRule = $rule;
 425                  }
 426                  if ( $matchRule )
 427                  {
 428                      if ( $mimeType )
 429                      {
 430                          if ( $wantedMimeData and
 431                               $matchRule['to'] == $wantedMimeData['name'] )
 432                              $mimeType = $matchRule['to'];
 433                      }
 434                      else
 435                          $mimeType = $matchRule['to'];
 436                  }
 437              }
 438          }
 439          if ( $mimeType )
 440          {
 441              $mimeData = eZMimeType::findByName( $mimeType );
 442              eZImageHandler::rewriteURL( $currentMimeData, $mimeData, $this->outputRewriteType(), $aliasName );
 443          }
 444          return $mimeData;
 445      }
 446  
 447      /*!
 448       \return the type of filename rewrite this handler uses for output.
 449      */
 450      function outputRewriteType()
 451      {
 452          return $this->OutputRewriteType;
 453      }
 454  
 455      /*!
 456       \return \c true if the filter \a $filter is supported by this handler.
 457      */
 458      function isFilterSupported( $filter )
 459      {
 460          return isset( $this->FilterMap[$filter['name']] );
 461      }
 462  
 463      /*!
 464       \pure
 465       Converts the source file \a $sourceMimeData to the destination file \a $destinationMimeData.
 466       If \a $filters is supplied then the filters will be applied to the conversion.
 467      */
 468      function convert( &$manager, $sourceMimeData, &$destinationMimeData, $filters = false )
 469      {
 470      }
 471  
 472  }
 473  
 474  /*!
 475    \class eZImageFactory ezimagehandler.php
 476    \brief Base class for image factories
 477  
 478    The image factory is responsible for producing image handlers
 479    when requested. This class must be inherited by specific
 480    factories to create specific handlers.
 481  */
 482  
 483  class eZImageFactory
 484  {
 485      /*!
 486       Initializes the factory with the name \a $name.
 487      */
 488      function eZImageFactory( $name )
 489      {
 490          $this->Name = $name;
 491      }
 492  
 493      /*!
 494       \return the name of the factory, this is the name referenced in the INI file.
 495      */
 496      function name()
 497      {
 498          return $this->Name;
 499      }
 500  
 501      /*!
 502       \pure
 503       Creates a new image handler from the INI group \a $iniGroup and optionally INI file \a $iniFilename.
 504       \note The default implementation returns \c null.
 505      */
 506      function &produceFromINI( $iniGroup, $iniFilename = false )
 507      {
 508          $imageHandler = null;
 509          return $imageHandler;
 510      }
 511  
 512      /// \privatesection
 513      var $Name;
 514  }
 515  
 516  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7