File modules/cms/model/Image.class.php

Last commit: Sun Jan 29 00:20:21 2023 +0100	Jan Dankert	New node type "Script".
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 use cms\base\Configuration; 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 int Neue Breite 87 * @param int Neue Hoehe 88 * @param float Bildgr��enfaktor 89 * @param int Altes Format als Integer-Konstante IMG_xxx 90 * @param int Neues Format als Integer-Konstante IMG_xxx 91 * @param int Jpeg-Qualitaet (sofern neues Format = Jpeg) 92 */ 93 function imageResize( $newWidth,$newHeight,$factor,$oldformat,$newformat,$jpegquality ) 94 { 95 $this->write(); // Datei schreiben 96 97 // Bildinformationen ermitteln 98 $size = getimagesize( $this->getCache()->getFilename() ); 99 100 // Breite und Hoehe des aktuellen Bildes 101 $oldWidth = $size[0]; 102 $oldHeight = $size[1]; 103 $aspectRatio = $oldHeight / $oldWidth; // Seitenverhaeltnis 104 105 // Wenn Breite und Hoehe fehlen, dann Bildgroesse beibehalten 106 if ( $newWidth == 0 && $newHeight == 0) 107 { 108 if ( $factor != 0 && $factor != 1 ) 109 { 110 $newWidth = $oldWidth * $factor; 111 $newHeight = $oldHeight * $factor; 112 $resizing = true; 113 } 114 else 115 { 116 $newWidth = $oldWidth; 117 $newHeight = $oldHeight; 118 $resizing = false; 119 } 120 } 121 else 122 { 123 $resizing = true; 124 } 125 126 // Wenn nur Breite oder Hoehe angegeben ist, dann 127 // das Seitenverhaeltnis beibehalten 128 if ( $newWidth == 0 ) 129 $newWidth = $newHeight / $aspectRatio; 130 131 if ( $newHeight == 0 ) 132 $newHeight = $newWidth * $aspectRatio; 133 134 135 switch( $oldformat ) 136 { 137 case IMG_GIF: // GIF 138 139 $oldImage = ImageCreateFromGIF( $this->tmpfile ); 140 break; 141 142 case IMG_JPG: // JPEG 143 144 $oldImage = ImageCreateFromJPEG($this->tmpfile); 145 break; 146 147 case IMG_PNG: // PNG 148 149 $oldImage = imagecreatefrompng($this->tmpfile); 150 break; 151 152 default: 153 throw new \LogicException('unsupported image format "'.$this->extension.'", cannot load image. resize failed'); 154 } 155 156 // Ab Version 2 der GD-Bibliothek sind TrueColor-Umwandlungen moeglich. 157 $hasTrueColor = Configuration::get(['image','truecolor']); 158 159 switch( $newformat ) 160 { 161 case IMG_GIF: // GIF 162 163 if ( $resizing ) 164 { 165 $newImage = ImageCreate($newWidth,$newHeight); 166 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth, 167 $newHeight,$oldWidth,$oldHeight); 168 } 169 else 170 { 171 $newImage = &$oldImage; 172 } 173 174 ImageGIF($newImage, $this->getCache()->getFilename() ); 175 $this->extension = 'gif'; 176 177 break; 178 179 case IMG_JPG: // JPEG 180 181 if ( !$resizing ) 182 { 183 $newImage = &$oldImage; 184 } 185 elseif ( $hasTrueColor ) 186 { 187 // Verwende TrueColor (GD2) 188 $newImage = imageCreateTrueColor( $newWidth,$newHeight ); 189 ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth, 190 $newHeight,$oldWidth,$oldHeight); 191 } 192 else 193 { 194 // GD Version 1.x unterstuetzt kein TrueColor 195 $newImage = ImageCreate($newWidth,$newHeight); 196 197 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth, 198 $newHeight,$oldWidth,$oldHeight); 199 } 200 201 ImageJPEG($newImage, $this->tmpfile,$jpegquality ); 202 $this->extension = 'jpeg'; 203 204 break; 205 206 case IMG_PNG: // PNG 207 208 if ( !$resizing ) 209 { 210 $newImage = &$oldImage; 211 } 212 elseif ( $hasTrueColor ) 213 { 214 // Verwende TrueColor (GD2) 215 $newImage = imageCreateTrueColor( $newWidth,$newHeight ); 216 217 ImageCopyResampled($newImage,$oldImage,0,0,0,0,$newWidth, 218 $newHeight,$oldWidth,$oldHeight); 219 } 220 else 221 { 222 // GD Version 1.x unterstuetzt kein TrueColor 223 $newImage = ImageCreate($newWidth,$newHeight); 224 225 ImageCopyResized($newImage,$oldImage,0,0,0,0,$newWidth, 226 $newHeight,$oldWidth,$oldHeight); 227 } 228 229 imagepng( $newImage,$this->getCache()->getFilename() ); 230 $this->extension = 'png'; 231 232 break; 233 234 default: 235 throw new \LogicException('unsupported image format "'.$newformat.'", cannot resize'); 236 } 237 238 $this->getCache()->invalidate(); 239 240 imagedestroy( $oldImage ); 241 //imagedestroy( $newImage ); 242 } 243 244 }
Download modules/cms/model/Image.class.php
History Sun, 29 Jan 2023 00:20:21 +0100 Jan Dankert New node type "Script". Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Sun, 1 Nov 2020 03:08:55 +0100 Jan Dankert Replaced the calls to "Configuration::rawConfig()" with the OO style calls; Cleanup LoginAction. Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.