Changeset 12775
- Timestamp:
- Dec 21, 2011, 2:51:44 PM (12 years ago)
- Location:
- extensions/derivatives
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/derivatives/i.php
r12770 r12775 105 105 106 106 $deriv = explode('_', $deriv); 107 foreach (ImageStdParams::get_ all_types() as $type)107 foreach (ImageStdParams::get_defined_type_map() as $type => $params) 108 108 { 109 109 if (substr($type,0,2) == $deriv[0]) 110 110 { 111 111 $page['derivative_type'] = $type; 112 $page['derivative_params'] = ImageStdParams::get_by_type($type);112 $page['derivative_params'] = $params; 113 113 break; 114 114 } -
extensions/derivatives/include/derivative.inc.php
r12770 r12775 4 4 { 5 5 public $rel_path; 6 6 7 public $coi=null; 7 8 private $size=null; 8 9 9 10 function __construct($infos) 10 11 { … … 27 28 $this->rel_path = get_themeconf('mime_icon_dir').strtolower($ext).'.png'; 28 29 } 29 30 30 31 $this->coi = @$infos['coi']; 31 32 if (isset($infos['width']) && isset($infos['height'])) … … 34 35 } 35 36 } 36 37 38 function has_size() 39 { 40 return $this->size != null; 41 } 42 37 43 function get_size() 38 44 { … … 43 49 } 44 50 51 52 45 53 final class DerivativeImage 46 54 { 55 const SAME_AS_SRC = 0x10; 56 47 57 public $src_image; 58 59 private $requested_type; 60 61 private $flags = 0; 48 62 private $params; 49 63 private $rel_path, $rel_url; 50 51 function __construct($type, $infos) 52 { 53 $this->src_image = new SrcImage($infos); 54 self::build($type, $src_image, $this->rel_path, $this->rel_url, $this->params); 55 } 56 64 65 function __construct($type, $src_image) 66 { 67 $this->src_image = $src_image; 68 if (is_string($type)) 69 { 70 $this->requested_type = $type; 71 $this->params = ImageStdParams::get_by_type($type); 72 } 73 else 74 { 75 $this->requested_type = IMG_CUSTOM; 76 $this->params = $type; 77 } 78 79 self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->flags); 80 } 81 57 82 static function thumb_url($infos) 58 83 { 59 84 $src_image = new SrcImage($infos); 60 self::build(IMG_THUMB, $src_image, $rel_path, $rel_url); 61 //die($rel_url); 85 self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url); 62 86 return get_root_url().$rel_url; 63 87 } … … 66 90 { 67 91 $src_image = new SrcImage($infos); 68 self::build($type, $src_image, $rel_path, $rel_url); 92 $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type; 93 self::build($src_image, $params, $rel_path, $rel_url); 69 94 return get_root_url().$rel_url; 70 95 } 71 72 private static function build($type, $src, &$rel_path, &$rel_url, &$params = null) 73 { 74 if (is_string($type)) 75 { 76 $params = ImageStdParams::get_by_type($type); 77 } 78 else 79 { 80 $type = IMG_CUSTOM; 96 97 static function get_all($infos) 98 { 99 $src_image = new SrcImage($infos); 100 $ret = array(); 101 foreach (ImageStdParams::get_defined_type_map() as $type => $params) 102 { 103 $derivative = new DerivativeImage($params, $src_image); 104 $ret[$type] = $derivative; 105 } 106 foreach (ImageStdParams::get_undefined_type_map() as $type => $type2) 107 { 108 $ret[$type] = $ret[$type2]; 81 109 } 82 110 83 // todo check if we should not downgrade to a different type because 84 // original file is too small ... 85 // we are using a mime type icon ... 86 // etc... 87 111 return $ret; 112 } 113 114 private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = null) 115 { 116 if ( $src->has_size() && $params->is_identity( $src->get_size() ) ) 117 { 118 // todo - what if we have a watermark maybe return a smaller size? 119 $flags |= self::SAME_AS_SRC; 120 $params = null; 121 $rel_path = $rel_url = $src->rel_path; 122 return; 123 } 124 88 125 $tokens=array(); 89 $tokens[] = substr($ type,0,2);90 126 $tokens[] = substr($params->type,0,2); 127 91 128 if (!empty($src->coi)) 92 129 { 93 130 $tokens[] = 'ci'.$src->coi; 94 131 } 95 96 if ($ type==IMG_CUSTOM)132 133 if ($params->type==IMG_CUSTOM) 97 134 { 98 135 $params->add_url_tokens($tokens); 99 136 } 100 137 101 138 $loc = $src->rel_path; 102 139 if (substr_compare($loc, '../', 0, 3)==0) … … 107 144 108 145 $rel_path = PWG_DERIVATIVE_DIR.$loc; 109 146 110 147 global $conf; 111 148 $url_style=$conf['derivative_url_style']; … … 122 159 } 123 160 } 124 161 125 162 if ($url_style == 2) 126 163 { … … 136 173 } 137 174 175 function get_url() 176 { 177 return get_root_url().$this->rel_url; 178 } 179 180 function same_as_source() 181 { 182 return $this->flags & self::SAME_AS_SRC; 183 } 184 185 138 186 /* returns the size of the derivative image*/ 139 187 function get_size() 140 188 { 189 if ($this->flags & self::SAME_AS_SRC) 190 { 191 return $this->src_image->get_size(); 192 } 141 193 return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi); 142 194 } 195 196 function get_size_htm() 197 { 198 $size = $this->get_size(); 199 if ($size) 200 { 201 return 'width="'.$size[0].'" height="'.$size[1].'"'; 202 } 203 } 204 205 function get_size_hr() 206 { 207 $size = $this->get_size(); 208 if ($size) 209 { 210 return $size[0].' x '.$size[1]; 211 } 212 } 213 143 214 } 144 215 -
extensions/derivatives/include/derivative_params.inc.php
r12770 r12775 30 30 { 31 31 public $l,$t,$r,$b; 32 32 33 33 function __construct($l) 34 34 { … … 37 37 $this->b = $l[1]; 38 38 } 39 39 40 40 function width() 41 41 { 42 42 return $this->r - $this->l; 43 43 } 44 44 45 45 function height() 46 46 { … … 53 53 return; 54 54 $tlcrop = floor($pixels/2); 55 55 56 56 if (!empty($coi)) 57 57 { … … 83 83 $this->r -= $pixels - $tlcrop; 84 84 } 85 85 86 86 function crop_v($pixels, $coi, $force) 87 87 { … … 89 89 return; 90 90 $tlcrop = floor($pixels/2); 91 91 92 92 if (!empty($coi)) 93 93 { … … 132 132 $this->min_size = $min_size; 133 133 } 134 134 135 135 static function classic($w, $h) 136 136 { … … 142 142 return new SizingParams( array($w,$w), 1, array($w,$w) ); 143 143 } 144 144 145 145 function add_url_tokens(&$tokens) 146 146 { 147 if ( !$this->could_crop())147 if ($this->max_crop == 0) 148 148 { 149 149 $tokens[] = 's'.size_to_url($this->ideal_size); … … 160 160 } 161 161 } 162 162 163 163 static function from_url_tokens($tokens) 164 164 { … … 175 175 return new SizingParams($s, 1, $s); 176 176 } 177 177 178 178 $ideal_size = url_to_size( $token ); 179 179 if (count($tokens)<2) 180 180 throw new Exception('Sizing arr'); 181 181 182 182 $token = array_shift($tokens); 183 183 $crop = sscanf('%02x' , $token) / 100; 184 184 185 185 $token = array_shift($tokens); 186 186 $min_size = url_to_size( $token ); 187 187 return new SizingParams($ideal_size, $crop, $min_size); 188 188 } 189 190 function could_crop() 191 { 192 return $this->max_crop > 0; 193 } 189 194 190 195 191 function compute($in_size, $coi, &$crop_rect, &$scale_size) 196 192 { 197 193 $destCrop = new ImageRect($in_size); 198 194 199 195 if ($this->max_crop > 0) 200 196 { … … 225 221 } 226 222 } 227 223 228 224 $scale_size = array($destCrop->width(), $destCrop->height()); 229 225 230 226 $ratio_w = $destCrop->width() / $this->ideal_size[0]; 231 227 $ratio_h = $destCrop->height() / $this->ideal_size[1]; … … 247 243 $scale_size = null; 248 244 } 249 245 250 246 $crop_rect = null; 251 247 if ($destCrop->width()!=$in_size[0] || $destCrop->height()!=$in_size[1] ) … … 261 257 final class ImageParams 262 258 { 259 public $type = IMG_CUSTOM; 263 260 public $sizing; 264 261 265 262 function __construct($sizing) 266 263 { 267 264 $this->sizing = $sizing; 268 265 } 269 266 270 267 function add_url_tokens(&$tokens) 271 268 { 272 269 $this->sizing->add_url_tokens($tokens); 273 270 } 274 271 275 272 static function from_url_tokens($tokens) 276 273 { … … 279 276 return $ret; 280 277 } 281 278 282 279 function compute_final_size($in_size, $coi) 283 280 { … … 285 282 return $scale_size != null ? $scale_size : $in_size; 286 283 } 284 285 function is_identity($in_size) 286 { 287 if ($in_size[0] > $this->sizing->ideal_size[0] or 288 $in_size[1] > $this->sizing->ideal_size[1] ) 289 { 290 return false; 291 } 292 return true; 293 } 287 294 } 288 295 ?> -
extensions/derivatives/include/derivative_std_params.inc.php
r12770 r12775 1 1 <?php 2 2 3 define('IMG_ TINY', 'tiny');3 define('IMG_SQUARE', 'square'); 4 4 define('IMG_THUMB', 'thumb'); 5 5 define('IMG_SMALL', 'small'); … … 12 12 final class ImageStdParams 13 13 { 14 private static $all_types = array(); 14 private static $all_types = array(IMG_SQUARE,IMG_THUMB,IMG_SMALL,IMG_MEDIUM,IMG_LARGE,IMG_XLARGE,IMG_XXLARGE); 15 private static $all_type_map = array(); 15 16 private static $type_map = array(); 17 private static $undefined_type_map = array(); 16 18 17 19 static function get_all_types() … … 20 22 } 21 23 24 static function get_all_type_map() 25 { 26 return self::$all_type_map; 27 } 28 29 static function get_defined_type_map() 30 { 31 return self::$type_map; 32 } 33 34 static function get_undefined_type_map() 35 { 36 return self::$undefined_type_map; 37 } 38 22 39 static function get_by_type($type) 23 40 { 24 return self::$ type_map[$type];41 return self::$all_type_map[$type]; 25 42 } 26 43 … … 28 45 { 29 46 self::make_default(); 47 self::build_maps(); 30 48 } 31 49 … … 33 51 { 34 52 self::make_default(); 53 self::build_maps(); 35 54 } 36 55 … … 38 57 { 39 58 //todo 40 self::$type_map[IMG_ TINY] = new ImageParams( SizingParams::square(100,100) );59 self::$type_map[IMG_SQUARE] = new ImageParams( SizingParams::square(100,100) ); 41 60 self::$type_map[IMG_THUMB] = new ImageParams( SizingParams::classic(144,144) ); 42 61 self::$type_map[IMG_SMALL] = new ImageParams( SizingParams::classic(240,240) ); 43 62 self::$type_map[IMG_MEDIUM] = new ImageParams( SizingParams::classic(432,432) ); 44 63 self::$type_map[IMG_LARGE] = new ImageParams( SizingParams::classic(864,648) ); 45 self::$ all_types = array_keys(self::$type_map);64 self::$type_map[IMG_XLARGE] = new ImageParams( SizingParams::classic(1200,900) ); 46 65 } 66 67 private static function build_maps() 68 { 69 foreach (self::$type_map as $type=>$params) 70 { 71 $params->type = $type; 72 } 73 self::$all_type_map = self::$type_map; 74 75 for ($i=0; $i<count(self::$all_types); $i++) 76 { 77 $tocheck = self::$all_types[$i]; 78 if (!isset(self::$type_map[$tocheck])) 79 { 80 for ($j=$i-1; $j>=0; $j--) 81 { 82 $target = self::$all_types[$j]; 83 if (isset(self::$type_map[$target])) 84 { 85 self::$all_type_map[$tocheck] = self::$type_map[$target]; 86 self::$undefined_type_map[$tocheck] = $target; 87 break; 88 } 89 } 90 } 91 } 92 } 93 47 94 } 48 95 -
extensions/derivatives/main.inc.php
r12770 r12775 24 24 } 25 25 26 27 add_event_handler( 28 'render_element_content', 29 'dyn_render_picture_content', 30 EVENT_HANDLER_PRIORITY_NEUTRAL-1, 31 2 32 ); 33 34 function dyn_render_picture_content($content, $element_info) 35 { 36 if ( !empty($content) ) 37 {// someone hooked us - so we skip; 38 return $content; 39 } 40 41 $all_derivatives = DerivativeImage::get_all($element_info); 42 //var_export($all_derivatives); 43 $selected_derivative = $all_derivatives[IMG_LARGE]; 44 45 $available_derivatives = array(); 46 $added = array(); 47 foreach($all_derivatives as $type => $derivative) 48 { 49 $url = $derivative->get_url(); 50 if (isset($added[$url])) 51 continue; 52 $added[$url] = 1; 53 $available_derivatives[] = $type; 54 } 55 56 57 global $user, $page, $template; 58 59 $template->set_filenames( 60 array('default_content'=> dirname(__FILE__).'/picture_content.tpl') 61 ); 62 63 $template->append('current', array( 64 'all_derivatives' => $all_derivatives, 65 'selected_derivative' => $selected_derivative, 66 'available_derivative_types' => $available_derivatives, 67 ), true); 68 69 $template->assign( array( 70 'ALT_IMG' => $element_info['file'] )); 71 72 return $template->parse( 'default_content', true);; 73 } 26 74 ?>
Note: See TracChangeset
for help on using the changeset viewer.