openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

ImageSizeAction.class.php (2864B)


      1 <?php
      2 namespace cms\action\image;
      3 use cms\action\Action;
      4 use cms\action\ImageAction;
      5 use cms\action\Method;
      6 use cms\model\Image;
      7 use cms\model\Name;
      8 use cms\model\Permission;
      9 use language\Messages;
     10 use util\exception\ValidationException;
     11 
     12 class ImageSizeAction extends ImageAction implements Method {
     13 	public function getRequiredPermission() {
     14 		return Permission::ACL_WRITE;
     15 	}
     16 
     17     public function view() {
     18 		$this->setTemplateVars( $this->image->getProperties() );
     19 		
     20 		$format = $this->imageFormat();
     21 
     22 		if	( $format == 0 )
     23 		{
     24 			$this->addWarningFor( $this->image,Messages::IMAGE_RESIZING_UNKNOWN_TYPE);
     25 		}
     26 			
     27 		$formats = $this->imageFormats();
     28 			
     29 		if	( empty($formats) )
     30 			$this->addWarningFor($this->image,Messages::IMAGE_RESIZING_NOT_AVAILABLE);
     31 		
     32 		$sizes = array();
     33 		foreach( array(10,25,50,75,100,125,150,175,200,250,300,350,400,500,600,800) as $s )
     34 			$sizes[strval($s/100)] = $s.'%';
     35 			
     36 		$jpeglist = array();
     37 		for ($i=10; $i<=95; $i+=5)
     38 			$jpeglist[$i]=$i.'%';
     39 
     40 		$this->setTemplateVar('factors'       ,$sizes      );
     41 		$this->setTemplateVar('jpeglist'      ,$jpeglist   );
     42 		$this->setTemplateVar('formats'       ,$formats    );
     43 		$this->setTemplateVar('format'        ,$format     );
     44 		$this->setTemplateVar('factor'        ,1           );
     45 		
     46 		$this->image->getImageSize();
     47 		$this->setTemplateVar('width' ,$this->image->width  );
     48 		$this->setTemplateVar('height',$this->image->height );
     49 		$this->setTemplateVar('type'  ,'input'             );
     50     }
     51 
     52 
     53     public function post() {
     54 		$width           = $this->request->getNumber('width'         );
     55 		$height          = $this->request->getNumber('height'        );
     56 		$jpegcompression = $this->request->getText('jpeg_compression');
     57 		$format          = $this->request->getText('format'          );
     58 		$factor          = $this->request->getText('factor'          );
     59 		
     60 		if	( $this->request->getText('type') == 'input' &&
     61 			  ! $width      &&
     62 			  ! $height )
     63 		{
     64 			$this->addWarningFor(null,Messages::INPUT_NEW_IMAGE_SIZE);
     65 			throw new ValidationException('width' );
     66 		}
     67 		
     68 		if	( $this->request->isTrue('copy') )
     69 		{
     70 			// Datei neu anlegen.
     71 			$imageFile = new Image();
     72 
     73 			$imageFile->filename   = $imageFile->filename.'_resized_'.time();
     74 			$imageFile->persist();
     75 			$imageFile->copyNamesFrom( $this->image->objectid );
     76 			$imageFile->copyValueFromFile( $this->image->objectid );
     77 		}
     78 		else
     79 		{
     80 			$imageFile = $this->image;
     81 		}
     82 		
     83 		if	( $this->request->getText('type') == 'factor')
     84 		{
     85 			$width  = 0;
     86 			$height = 0;
     87 		}
     88 		else
     89 		{
     90 			$factor = 1;
     91 		}
     92 
     93 		$imageFile->write();
     94 		
     95 		$imageFile->imageResize( intval($width),intval($height),$factor,$this->imageFormat(),$format,$jpegcompression );
     96 		$imageFile->setTimestamp();
     97 		$imageFile->save();      // Um z.B. Groesse abzuspeichern
     98 		$imageFile->saveValue();
     99 
    100 		$this->addNoticeFor($imageFile,Messages::IMAGE_RESIZED);
    101     }
    102 }