openrat-cms

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

Image.class.php (5851B)


      1 <?php
      2 namespace cms\model;
      3 // OpenRat Content Management System
      4 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      5 //
      6 // This program is free software; you can redistribute it and/or
      7 // modify it under the terms of the GNU General Public License
      8 // as published by the Free Software Foundation; either version 2
      9 // of the License, or (at your option) any later version.
     10 //
     11 // This program is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 //
     16 // You should have received a copy of the GNU General Public License
     17 // along with this program; if not, write to the Free Software
     18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     19 
     20 
     21 
     22 /**
     23  * Datei.
     24  *
     25  * @author Jan Dankert
     26  * @package openrat.objects
     27  */
     28 class Image extends File
     29 {
     30 	/**
     31 	 * Breite eines Bildes. Ist nur verfuegbar, wenn vorher
     32 	 * #getImageSize() aufgerufen wurde.
     33 	 */
     34 	var $width         = null;
     35 	
     36 	/**
     37 	 * Hoehe eines Bildes. Ist nur verfuegbar, wenn vorher
     38 	 * #getImageSize() aufgerufen wurde.
     39 	 */
     40 	var $height        = null;
     41 	
     42 
     43 	/**
     44 	 * Konstruktor
     45 	 *
     46 	 * @param Objekt-Id
     47 	 */
     48 	function __construct( $objectid='' )
     49 	{
     50 	    parent::__construct($objectid);
     51 
     52         $this->isImage = true;
     53         $this->isFile  = false;
     54 		$this->typeid = BaseObject::TYPEID_FILE;
     55 	}
     56 
     57 
     58 
     59 
     60 	/**
     61 	 * Ermittelt Breite und H�he des Bildes.<br>
     62 	 * Die Werte lassen sich anschlie�end �ber die Eigenschaften "width" und "height" ermitteln.
     63 	 */
     64 	function getImageSize()
     65 	{
     66 		if	( is_null($this->width) )
     67 		{
     68 			$this->write(); // Datei schreiben
     69 			
     70 			// Bildinformationen ermitteln
     71 			$size = getimagesize( $this->getCache()->getFilename() );
     72 	
     73 			// Breite und Hoehe des aktuellen Bildes	 
     74 			$this->width  = $size[0]; 
     75 			$this->height = $size[1];
     76 		}
     77 	}
     78 
     79 
     80 
     81 	/**
     82 	 * Veraendert die Bildgroesse eines Bildes
     83 	 *
     84 	 * Diese Methode sollte natuerlich nur bei Bildern ausgefuehrt werden.
     85 	 *
     86 	 * @param Neue Breite
     87 	 * @param Neue Hoehe
     88 	 * @param Bildgr��enfaktor
     89 	 * @param Altes Format als Integer-Konstante IMG_xxx
     90 	 * @param Neues Format als Integer-Konstante IMG_xxx
     91 	 * @param Jpeg-Qualitaet (sofern neues Format = Jpeg)
     92 	 */
     93 	function imageResize( $newWidth,$newHeight,$factor,$oldformat,$newformat,$jpegquality )
     94 	{
     95 		global $conf;
     96 
     97 		$this->write(); // Datei schreiben
     98 		
     99 		// Bildinformationen ermitteln
    100 		$size = getimagesize( $this->getCache()->getFilename() );
    101 
    102 		// Breite und Hoehe des aktuellen Bildes	 
    103 		$oldWidth  = $size[0]; 
    104 		$oldHeight = $size[1];
    105 		$aspectRatio = $oldHeight / $oldWidth; // Seitenverhaeltnis
    106 
    107 		// Wenn Breite und Hoehe fehlen, dann Bildgroesse beibehalten
    108 		if	( $newWidth == 0 && $newHeight == 0)
    109 		{
    110 			if	( $factor != 0 && $factor != 1 )
    111 			{
    112 				$newWidth  = $oldWidth  * $factor; 
    113 				$newHeight = $oldHeight * $factor;
    114 				$resizing = true;
    115 			}
    116 			else
    117 			{
    118 				$newWidth  = $oldWidth; 
    119 				$newHeight = $oldHeight;
    120 				$resizing = false;
    121 			}
    122 		}
    123 		else
    124 		{
    125 			$resizing = true;
    126 		}
    127 
    128 		// Wenn nur Breite oder Hoehe angegeben ist, dann
    129 		// das Seitenverhaeltnis beibehalten
    130 		if	( $newWidth == 0 )
    131 			$newWidth = $newHeight / $aspectRatio; 
    132 		
    133 		if	( $newHeight == 0 )
    134 			$newHeight = $newWidth * $aspectRatio; 
    135 
    136 
    137 		switch( $oldformat )
    138 		{
    139 			case IMG_GIF: // GIF
    140 
    141 				$oldImage = ImageCreateFromGIF( $this->tmpfile ); 
    142 				break;
    143 
    144 			case IMG_JPG: // JPEG
    145 
    146 				$oldImage = ImageCreateFromJPEG($this->tmpfile);
    147 				break;
    148 
    149 			case IMG_PNG: // PNG
    150 
    151 				$oldImage = imagecreatefrompng($this->tmpfile);
    152 				break;
    153 
    154 			default:
    155 				throw new \LogicException('unsupported image format "'.$this->extension.'", cannot load image. resize failed');
    156 		}
    157 
    158 		// Ab Version 2 der GD-Bibliothek sind TrueColor-Umwandlungen moeglich.
    159 		global $conf;
    160  		$hasTrueColor = $conf['image']['truecolor'];
    161 
    162 		switch( $newformat )
    163 		{
    164 			case IMG_GIF: // GIF
    165 
    166 				if	( $resizing )
    167 				{
    168 					$newImage = ImageCreate($newWidth,$newHeight); 
    169 					ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
    170 						$newHeight,$oldWidth,$oldHeight); 
    171 				}
    172 				else
    173 				{
    174 					$newImage = &$oldImage;
    175 				} 
    176 
    177 				ImageGIF($newImage, $this->getCache()->getFilename() );
    178 				$this->extension = 'gif';
    179 
    180 				break;
    181 
    182 			case IMG_JPG: // JPEG
    183 
    184 				if	( !$resizing )
    185 				{
    186 					$newImage = &$oldImage;
    187 				} 
    188 				elseif   ( $hasTrueColor )
    189 				{
    190 					// Verwende TrueColor (GD2)
    191 					$newImage = imageCreateTrueColor( $newWidth,$newHeight );
    192 					ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth,
    193 					$newHeight,$oldWidth,$oldHeight);
    194 				}
    195 				else
    196 				{
    197 					// GD Version 1.x unterstuetzt kein TrueColor
    198 					$newImage = ImageCreate($newWidth,$newHeight);
    199 	
    200 					ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
    201 					$newHeight,$oldWidth,$oldHeight);
    202 				}
    203 	
    204 				ImageJPEG($newImage, $this->tmpfile,$jpegquality ); 
    205 				$this->extension = 'jpeg';
    206 
    207 				break;
    208 
    209 			case IMG_PNG: // PNG
    210 
    211 				if	( !$resizing )
    212 				{
    213 					$newImage = &$oldImage;
    214 				} 
    215 				elseif   ( $hasTrueColor )
    216 				{
    217 					// Verwende TrueColor (GD2)
    218 					$newImage = imageCreateTrueColor( $newWidth,$newHeight );
    219 		
    220 					ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth,
    221 					$newHeight,$oldWidth,$oldHeight); 
    222 				}
    223 				else
    224 				{
    225 					// GD Version 1.x unterstuetzt kein TrueColor
    226 					$newImage = ImageCreate($newWidth,$newHeight);
    227 		
    228 					ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth,
    229 					$newHeight,$oldWidth,$oldHeight); 
    230 				}
    231 		
    232 				imagepng( $newImage,$this->getCache()->getFilename() );
    233 				$this->extension = 'png';
    234 				
    235 				break;
    236 				
    237 			default:
    238 				throw new \LogicException('unsupported image format "'.$newformat.'", cannot resize');
    239 		} 
    240 
    241 		$this->getCache()->invalidate();
    242 
    243 		imagedestroy( $oldImage );
    244 		//imagedestroy( $newImage );
    245 	}
    246 
    247 }
    248 
    249 ?>