openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

ImageAction.class.php (5002B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 use cms\model\Folder;
      6 use cms\model\Image;
      7 use cms\model\BaseObject;
      8 use cms\model\File;
      9 
     10 use Http;
     11 use \Html;
     12 use Upload;
     13 
     14 
     15 /**
     16  * Action-Klasse zum Bearbeiten eines Bildes.
     17  * @author Jan Dankert
     18  * @version $Revision$
     19  * @package openrat.actions
     20  */
     21 class ImageAction extends FileAction
     22 {
     23 	public $security = Action::SECURITY_USER;
     24 
     25 	var $image;
     26 
     27 	/**
     28 	 * Konstruktor
     29 	 */
     30 	public function __construct()
     31 	{
     32         parent::__construct();
     33 
     34     }
     35 
     36 
     37     public function init()
     38     {
     39 		$image = new Image( $this->getRequestId() );
     40 		$image->load();
     41 
     42         $this->setBaseObject( $image );
     43 
     44     }
     45 
     46 
     47     protected function setBaseObject( $image ) {
     48 		$this->image = $image;
     49 
     50 		parent::setBaseObject($image);
     51 	}
     52 
     53 	
     54 	/**
     55 	 * Anzeigen des Inhaltes
     56 	 */
     57     public function sizeView()
     58 	{
     59 		$this->setTemplateVars( $this->image->getProperties() );
     60 		
     61 		$format = $this->imageFormat();
     62 
     63 		if	( $format == 0 )
     64 		{
     65 			$this->addNotice( 'image','','IMAGE_RESIZING_UNKNOWN_TYPE',OR_NOTICE_WARN);
     66 		}
     67 			
     68 		$formats = $this->imageFormats();
     69 			
     70 		if	( empty($formats) )
     71 			$this->addNotice( 'image','','IMAGE_RESIZING_NOT_AVAILABLE',OR_NOTICE_WARN);
     72 		
     73 		$sizes = array();
     74 		foreach( array(10,25,50,75,100,125,150,175,200,250,300,350,400,500,600,800) as $s )
     75 			$sizes[strval($s/100)] = $s.'%';
     76 			
     77 		$jpeglist = array();
     78 		for ($i=10; $i<=95; $i+=5)
     79 			$jpeglist[$i]=$i.'%';
     80 
     81 		$this->setTemplateVar('factors'       ,$sizes      );
     82 		$this->setTemplateVar('jpeglist'      ,$jpeglist   );
     83 		$this->setTemplateVar('formats'       ,$formats    );
     84 		$this->setTemplateVar('format'        ,$format     );
     85 		$this->setTemplateVar('factor'        ,1           );
     86 		
     87 		$this->image->getImageSize();
     88 		$this->setTemplateVar('width' ,$this->image->width  );
     89 		$this->setTemplateVar('height',$this->image->height );
     90 		$this->setTemplateVar('type'  ,'input'             );
     91 	}
     92 
     93 
     94 	
     95 
     96 	/**
     97 	 * Bildgroesse eines Bildes aendern
     98 	 */
     99 	public function sizePost()
    100 	{
    101 		$width           = intval($this->getRequestVar('width'           ));
    102 		$height          = intval($this->getRequestVar('height'          ));
    103 		$jpegcompression =        $this->getRequestVar('jpeg_compression') ;
    104 		$format          =        $this->getRequestVar('format'          ) ;
    105 		$factor          =        $this->getRequestVar('factor'          ) ;
    106 		
    107 		if	( $this->getRequestVar('type') == 'input' &&
    108 			  ! $this->hasRequestVar('width' )      &&
    109 			  ! $this->hasRequestVar('height') )
    110 		{
    111 			$this->addValidationError('width','INPUT_NEW_IMAGE_SIZE' );
    112 			$this->addValidationError('height','');
    113 			$this->callSubAction('size');
    114 			return;
    115 		}
    116 		
    117 		if	( $this->hasRequestVar('copy') )
    118 		{
    119 			// Datei neu anlegen.
    120 			$imageFile = new Image($this->image->objectid);
    121 			$imageFile->load();
    122 			$imageFile->name       = lang('copy_of').' '.$imageFile->name;
    123 			$imageFile->desription = lang('copy_of').' '.$imageFile->description;
    124 			$imageFile->filename   = $imageFile->filename.'_resized_'.time();
    125 			$imageFile->add();
    126 			$imageFile->copyValueFromFile( $this->image->objectid );
    127 		}
    128 		else
    129 		{
    130 			$imageFile = $this->image;
    131 		}
    132 		
    133 		if	( $this->getRequestVar('type') == 'factor')
    134 		{
    135 			$width  = 0;
    136 			$height = 0;
    137 		}
    138 		else
    139 		{
    140 			$factor = 1;
    141 		}
    142 
    143 		$imageFile->write();
    144 		
    145 		$imageFile->imageResize( intval($width),intval($height),$factor,$this->imageFormat(),$format,$jpegcompression );
    146 		$imageFile->setTimestamp();
    147 		$imageFile->save();      // Um z.B. Groesse abzuspeichern
    148 		$imageFile->saveValue();
    149 
    150 		$this->addNotice($imageFile->getType(),$imageFile->name,'IMAGE_RESIZED','ok');
    151 	}
    152 
    153 
    154 
    155     private function imageFormat()
    156     {
    157         if	( ! function_exists( 'imagetypes' ) )
    158             return 0;
    159 
    160         $ext      = strtolower($this->image->getRealExtension());
    161         $types    = imagetypes();
    162         $formats  = array( 'gif' =>IMG_GIF,
    163             'jpg' =>IMG_JPG,
    164             'jpeg'=>IMG_JPG,
    165             'png' =>IMG_PNG );
    166 
    167         if	( !isset($formats[$ext]) )
    168             return 0;
    169 
    170         if	( $types & $formats[$ext] )
    171             return $formats[$ext];
    172 
    173         return 0;
    174     }
    175 
    176 
    177 
    178     private function imageExt()
    179     {
    180         switch( $this->imageFormat() )
    181         {
    182             case IMG_GIF:
    183                 return 'GIF';
    184             case IMG_JPG:
    185                 return 'JPEG';
    186             case IMG_PNG:
    187                 return 'PNG';
    188         }
    189     }
    190 
    191 
    192 
    193     private function imageFormats()
    194     {
    195         if	( ! function_exists( 'imagetypes' ) )
    196             return array();
    197 
    198         $types    = imagetypes();
    199         $formats  = array( IMG_GIF => 'gif',
    200             IMG_JPG => 'jpeg',
    201             IMG_PNG => 'png' );
    202         $formats2 = $formats;
    203 
    204         foreach( $formats as $b=>$f )
    205             if	( !($types & $b) )
    206                 unset( $formats2[$b] );
    207 
    208         return $formats2;
    209     }
    210 
    211 
    212     /**
    213      * Anzeigen des Inhaltes, der Inhalt wird samt Header direkt
    214      * auf die Standardausgabe geschrieben
    215      */
    216     public function previewView()
    217     {
    218         parent::previewView();
    219     }
    220 
    221 
    222 
    223 
    224 }