| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: field.class.php,v 1.24.2.2 2008/07/10 09:48:45 scyrma Exp $ 2 /////////////////////////////////////////////////////////////////////////// 3 // // 4 // NOTICE OF COPYRIGHT // 5 // // 6 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 7 // http://moodle.org // 8 // // 9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com // 10 // // 11 // This program is free software; you can redistribute it and/or modify // 12 // it under the terms of the GNU General Public License as published by // 13 // the Free Software Foundation; either version 2 of the License, or // 14 // (at your option) any later version. // 15 // // 16 // This program is distributed in the hope that it will be useful, // 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 19 // GNU General Public License for more details: // 20 // // 21 // http://www.gnu.org/copyleft/gpl.html // 22 // // 23 /////////////////////////////////////////////////////////////////////////// 24 25 require_once($CFG->dirroot.'/mod/data/field/file/field.class.php'); 26 // Base class is 'file' 27 28 class data_field_picture extends data_field_file { 29 var $type = 'picture'; 30 var $previewwidth = 50; 31 var $previewheight = 50; 32 33 function data_field_picture($field=0, $data=0) { 34 parent::data_field_base($field, $data); 35 } 36 37 function display_add_field($recordid=0) { 38 global $CFG; 39 $filepath = ''; 40 $filename = ''; 41 $description = ''; 42 if ($recordid) { 43 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) { 44 $filename = $content->content; 45 $description = $content->content1; 46 } 47 $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; 48 require_once($CFG->libdir.'/filelib.php'); 49 $filepath = get_file_url("$path/$filename"); 50 } 51 $str = '<div title="'.s($this->field->description).'">'; 52 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; 53 $str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" id="field_'.$this->field->id.'_file" value="fakevalue" />'; 54 $str .= '<label for="field_'.$this->field->id.'">'.get_string('picture','data'). '</label> <input type="file" name ="field_'.$this->field->id.'" id="field_'.$this->field->id.'" /><br />'; 55 $str .= '<label for="field_'.$this->field->id.'_filename">'.get_string('alttext','data') .'</label> <input type="text" name="field_' 56 .$this->field->id.'_filename" id="field_'.$this->field->id.'_filename" value="'.s($description).'" /><br />'; 57 $str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />'; 58 if ($filepath) { 59 $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$filepath.'" alt="" />'; 60 } 61 $str .= '</fieldset>'; 62 $str .= '</div>'; 63 return $str; 64 } 65 66 function display_search_field($value = '') { 67 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />'; 68 } 69 70 function parse_search_field() { 71 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); 72 } 73 74 function generate_sql($tablealias, $value) { 75 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') "; 76 } 77 78 function display_browse_field($recordid, $template) { 79 global $CFG; 80 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){ 81 if (isset($content->content)) { 82 $contents[0] = $content->content; 83 $contents[1] = $content->content1; 84 } 85 if (empty($contents[0])) { 86 // Nothing to show 87 return ''; 88 } 89 $alt = empty($contents[1])? '':$contents[1]; 90 $title = empty($contents[1])? '':$contents[1]; 91 $src = $contents[0]; 92 $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; 93 94 $thumbnaillocation = $CFG->dataroot .'/'. $path .'/thumb/'.$src; 95 require_once($CFG->libdir.'/filelib.php'); 96 $source = get_file_url("$path/$src"); 97 $thumbnailsource = file_exists($thumbnaillocation) ? get_file_url("$path/thumb/$src") : $source; 98 99 if ($template == 'listtemplate') { 100 $width = $this->field->param4 ? ' width="'.s($this->field->param4).'" ' : ' '; 101 $height = $this->field->param5 ? ' height="'.s($this->field->param5).'" ' : ' '; 102 $str = '<a href="view.php?d='.$this->field->dataid.'&rid='.$recordid.'"><img '. 103 $width.$height.' src="'.$thumbnailsource.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; 104 } else { 105 $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; 106 $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; 107 $str = '<a href="'.$source.'"><img '.$width.$height.' src="'.$source.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; 108 } 109 return $str; 110 } 111 return false; 112 } 113 114 function update_field() { 115 // Get the old field data so that we can check whether the thumbnail dimensions have changed 116 $oldfield = get_record('data_fields', 'id', $this->field->id); 117 if (!update_record('data_fields', $this->field)) { 118 notify('updating of new field failed!'); 119 return false; 120 } 121 122 // Have the thumbnail dimensions changed? 123 if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { 124 // Check through all existing records and update the thumbnail 125 if ($contents = get_records('data_content', 'fieldid', $this->field->id)) { 126 if (count($contents) > 20) { 127 notify(get_string('resizingimages', 'data'), 'notifysuccess'); 128 echo "\n\n"; 129 // To make sure that ob_flush() has the desired effect 130 ob_flush(); 131 } 132 foreach ($contents as $content) { 133 @set_time_limit(300); 134 // Might be slow! 135 $this->update_thumbnail($content); 136 } 137 } 138 } 139 return true; 140 } 141 142 function update_content($recordid, $value, $name) { 143 parent::update_content($recordid, $value, $name); 144 $content = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid); 145 $this->update_thumbnail($content); 146 // Regenerate the thumbnail 147 } 148 149 function update_thumbnail($content) { 150 // (Re)generate thumbnail image according to the dimensions specified in the field settings. 151 // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and 152 // additionally an attempted delete of the existing thumbnail takes place. 153 global $CFG; 154 require_once($CFG->libdir . '/gdlib.php'); 155 $datalocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'. 156 $this->data->id.'/'.$this->field->id.'/'.$content->recordid; 157 $originalfile = $datalocation.'/'.$content->content; 158 if (!file_exists($originalfile)) { 159 return; 160 } 161 if (!file_exists($datalocation.'/thumb')) { 162 mkdir($datalocation.'/thumb', 0777); 163 // robertall: Why hardcode 0777?? 164 } 165 $thumbnaillocation = $datalocation.'/thumb/'.$content->content; 166 $imageinfo = GetImageSize($originalfile); 167 $image->width = $imageinfo[0]; 168 $image->height = $imageinfo[1]; 169 $image->type = $imageinfo[2]; 170 if (!$image->width || !$image->height) { 171 // Should not happen 172 return; 173 } 174 switch ($image->type) { 175 case 1: 176 if (function_exists('ImageCreateFromGIF')) { 177 $im = ImageCreateFromGIF($originalfile); 178 } else { 179 return; 180 } 181 break; 182 case 2: 183 if (function_exists('ImageCreateFromJPEG')) { 184 $im = ImageCreateFromJPEG($originalfile); 185 } else { 186 return; 187 } 188 break; 189 case 3: 190 if (function_exists('ImageCreateFromPNG')) { 191 $im = ImageCreateFromPNG($originalfile); 192 } else { 193 return; 194 } 195 break; 196 } 197 $thumbwidth = isset($this->field->param4)?$this->field->param4:''; 198 $thumbheight = isset($this->field->param5)?$this->field->param5:''; 199 if ($thumbwidth || $thumbheight) { 200 // Only if either width OR height specified do we want a thumbnail 201 $wcrop = $image->width; 202 $hcrop = $image->height; 203 if ($thumbwidth && !$thumbheight) { 204 $thumbheight = $image->height * $thumbwidth / $image->width; 205 } else if($thumbheight && !$thumbwidth) { 206 $thumbwidth = $image->width * $thumbheight / $image->height; 207 } else { 208 // BOTH are set - may need to crop if aspect ratio differs 209 $hratio = $image->height / $thumbheight; 210 $wratio = $image->width / $thumbwidth; 211 if ($wratio > $hratio) { 212 // Crop the width 213 $wcrop = intval($thumbwidth * $hratio); 214 } elseif($hratio > $wratio) { 215 //Crop the height 216 $hcrop = intval($thumbheight * $wratio); 217 } 218 } 219 220 // At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop 221 222 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { 223 $im1 = ImageCreateTrueColor($thumbwidth,$thumbheight); 224 } else { 225 $im1 = ImageCreate($thumbwidth,$thumbheight); 226 } 227 if ($image->type == 3) { 228 // Prevent alpha blending for PNG images 229 imagealphablending($im1, false); 230 } 231 $cx = $image->width / 2; 232 $cy = $image->height / 2; 233 234 // These "half" measurements use the "crop" values rather than the actual dimensions 235 $halfwidth = floor($wcrop * 0.5); 236 $halfheight = floor($hcrop * 0.5); 237 238 ImageCopyBicubic($im1, $im, 0, 0, $cx-$halfwidth, $cy-$halfheight, 239 $thumbwidth, $thumbheight, $halfwidth*2, $halfheight*2); 240 241 if ($image->type == 3) { 242 // Save alpha transparency for PNG images 243 imagesavealpha($im1, true); 244 } 245 if (function_exists('ImageJpeg') && $image->type != 3) { 246 @touch($thumbnaillocation); 247 // Helps in Safe mode 248 if (ImageJpeg($im1, $thumbnaillocation, 90)) { 249 @chmod($thumbnaillocation, 0666); 250 // robertall: Why hardcode 0666?? 251 } 252 } elseif (function_exists('ImagePng') && $image->type == 3) { 253 @touch($thumbnaillocation); 254 // Helps in Safe mode 255 if (ImagePng($im1, $thumbnaillocation, 9)) { 256 @chmod($thumbnaillocation, 0666); 257 // robertall: Why hardcode 0666?? 258 } 259 } 260 } else { 261 // Try to remove the thumbnail - we don't want thumbnailing active 262 @unlink($thumbnaillocation); 263 } 264 } 265 266 function text_export_supported() { 267 return false; 268 } 269 270 } 271 272 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |