[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * This file implements various Image File handling functions. 4 * 5 * This file is part of the evoCore framework - {@link http://evocore.net/} 6 * See also {@link http://sourceforge.net/projects/evocms/}. 7 * 8 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 9 * 10 * {@internal License choice 11 * - If you have received this file as part of a package, please find the license.txt file in 12 * the same folder or the closest folder above for complete license terms. 13 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/) 14 * then you must choose one of the following licenses before using the file: 15 * - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php 16 * - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php 17 * }} 18 * 19 * {@internal Open Source relicensing agreement: 20 * }} 21 * 22 * @package evocore 23 * 24 * {@internal Below is a list of authors who have contributed to design/coding of this file: }} 25 * @author fplanque: Francois PLANQUE. 26 * 27 * @version $Id: _image.funcs.php,v 1.1 2007/06/25 10:59:57 fplanque Exp $ 28 */ 29 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 30 31 32 /** 33 * Scale dimensions to fit into a constrained size, while preserving aspect ratio. 34 * 35 * @param integer source width 36 * @param integer source height 37 * @param integer constrained width 38 * @param integer constrained height 39 */ 40 function scale_to_constraint( $src_width, $src_height, $max_width, $max_height ) 41 { 42 $src_ratio = $src_width / $src_height; 43 if( $max_width / $max_height <= $src_ratio ) 44 { 45 $width = $max_width; 46 $height = (int)round( $max_width / $src_ratio ); 47 } 48 else 49 { 50 $width = (int)round( $max_height * $src_ratio ); 51 $height = $max_height; 52 } 53 54 return array( $width, $height ); 55 } 56 57 58 /** 59 * Scale dimensions to fit into a constrained size, while preserving aspect ratio. 60 * The scaling only happens if the source is larger than the constraint. 61 * 62 * @param integer source width 63 * @param integer source height 64 * @param integer constrained width 65 * @param integer constrained height 66 */ 67 function fit_into_constraint( $src_width, $src_height, $max_width, $max_height ) 68 { 69 if( $src_width > $max_width || $src_height > $max_height ) 70 { 71 return scale_to_constraint( $src_width, $src_height, $max_width, $max_height ); 72 } 73 74 return array( $src_width, $src_height ); 75 } 76 77 78 /** 79 * Load an image from a file into memory 80 * 81 * @param string pathname of image file 82 * @param string 83 * @return array resource image handle or NULL 84 */ 85 function load_image( $path, $mimetype ) 86 { 87 $err = NULL; 88 $err_info = NULL; 89 $imh = NULL; 90 91 switch( $mimetype ) 92 { 93 case 'image/jpeg': 94 $imh = imagecreatefromjpeg( $path ); // dh> TODO: this can fail, if $path is not a valid jpeg! Handle this. 95 break; 96 97 case 'image/gif': 98 $imh = imagecreatefromgif( $path ); // dh> TODO: this can fail, if $path is not a valid gif! Handle this. 99 break; 100 101 default: 102 // Unrecognized mime type 103 $err = 'Emime'; // Sort error code 104 $err_info = $mimetype; 105 break; 106 } 107 108 return array( $err, $err_info, $imh ); 109 } 110 111 112 /** 113 * Output an image from memory to web client 114 * 115 * @param resource image handle 116 * @param string pathname of image file 117 * @param string 118 * @param integer 119 * @param string permissions 120 * @return string 121 */ 122 function save_image( $imh, $path, $mimetype, $quality = 90, $chmod = NULL ) 123 { 124 $err = NULL; 125 126 switch( $mimetype ) 127 { 128 case 'image/jpeg': 129 imagejpeg( $imh, $path, $quality ); 130 break; 131 132 case 'image/gif': 133 imagegif( $imh, $path ); 134 break; 135 136 default: 137 // Unrecognized mime type 138 $err = 'Emime'; // Sort error code 139 break; 140 } 141 142 if( empty( $err ) ) 143 { 144 // Make sure the file has the default permissions we want: 145 if( $chmod === NULL ) 146 { 147 global $Settings; 148 $chmod = $Settings->get('fm_default_chmod_file'); 149 } 150 chmod( $path, octdec( $chmod ) ); 151 } 152 153 return $err; 154 } 155 156 157 /** 158 * Output an image from memory to web client 159 * 160 * @param resource image handle 161 * @param string 162 * @return string 163 */ 164 function output_image( $imh, $mimetype ) 165 { 166 $err = NULL; 167 168 switch( $mimetype ) 169 { 170 case 'image/jpeg': 171 header('Content-type: '.$mimetype ); 172 imagejpeg( $imh ); 173 break; 174 175 case 'image/gif': 176 header('Content-type: '.$mimetype ); 177 imagegif( $imh ); 178 break; 179 180 default: 181 // Unrecognized mime type 182 $err = 'Emime'; // Sort error code 183 break; 184 } 185 186 return $err; 187 } 188 189 190 191 192 /** 193 * Generate a thumbnail 194 * 195 * @return array short error code + dest image handler 196 */ 197 function generate_thumb( $src_imh, $thumb_width, $thumb_height ) 198 { 199 $src_width = imagesx( $src_imh ) ; 200 $src_height = imagesy( $src_imh ); 201 202 if( $src_width <= $thumb_width && $src_height <= $thumb_height ) 203 { // There is no need to resample, use original! 204 return array( NULL, $src_imh ); 205 } 206 207 list( $dest_width, $dest_height ) = scale_to_constraint( $src_width, $src_height, $thumb_width, $thumb_height ); 208 209 // pre_dump( $src_width, $src_height, $dest_width, $dest_height ); 210 211 $dest_imh = imagecreatetruecolor( $dest_width, $dest_height ); // Create a black image 212 if( ! imagecopyresampled( $dest_imh, $src_imh, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height ) ) 213 { 214 return array( 'Eresample', $dest_imh ); 215 } 216 217 // TODO: imageinterlace(); 218 219 return array( NULL, $dest_imh ); 220 } 221 222 /* 223 * $Log: _image.funcs.php,v $ 224 * Revision 1.1 2007/06/25 10:59:57 fplanque 225 * MODULES (refactored MVC) 226 * 227 * Revision 1.9 2007/05/28 11:28:22 fplanque 228 * file perm fix / thumbnails 229 * 230 * Revision 1.8 2007/04/26 00:11:10 fplanque 231 * (c) 2007 232 * 233 * Revision 1.7 2007/03/20 07:39:08 fplanque 234 * filemanager fixes, including the chmod octal stuff 235 * 236 * Revision 1.6 2007/03/08 00:22:35 blueyed 237 * TODO 238 * 239 * Revision 1.5 2007/01/19 08:20:36 fplanque 240 * Addressed resized image quality. 241 * 242 * Revision 1.4 2006/12/13 22:43:24 fplanque 243 * default perms for newly created thumbnails 244 * 245 * Revision 1.3 2006/12/13 21:23:56 fplanque 246 * .evocache folders / saving of thumbnails 247 * 248 * Revision 1.2 2006/12/13 20:10:31 fplanque 249 * object responsibility delegation? 250 * 251 * Revision 1.1 2006/12/13 18:10:21 fplanque 252 * thumbnail resampling proof of concept 253 * 254 */ 255 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |