dirroot.'/mod/data/field/file/field.class.php'); // Base class is 'file' class data_field_picture extends data_field_file { var $type = 'picture'; var $previewwidth = 50; var $previewheight = 50; function data_field_picture($field=0, $data=0) { parent::data_field_base($field, $data); } function display_add_field($recordid=0){ global $CFG; $filepath = ''; $filename = ''; $description = ''; if ($recordid){ if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) { $filename = $content->content; $description = $content->content1; } $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; if ($CFG->slasharguments) { $filepath = $CFG->wwwroot.'/file.php/'.$path.'/'.$filename; } else { $filepath = $CFG->wwwroot.'/file.php?file=/'.$path.'/'.$filename; } } $str = '
'; $str .= '
'.$this->field->name.''; $str .= ''; $str .= ' 
'; $str .= ' 
'; $str .= ''; if ($filepath){ $str .= ''; } $str .= '
'; $str .= '
'; return $str; } function display_browse_field($recordid, $template) { global $CFG; if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){ if (isset($content->content)){ $contents[0] = $content->content; $contents[1] = $content->content1; } if (empty($contents[0])) { // Nothing to show return ''; } $alt = empty($contents[1])? '':$contents[1]; $title = empty($contents[1])? '':$contents[1]; $src = $contents[0]; $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; $thumbnaillocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid.'/thumb/'.$src; if ($CFG->slasharguments) { $source = $CFG->wwwroot.'/file.php/'.$path.'/'.$src; $thumbnailsource = file_exists($thumbnaillocation) ? $CFG->wwwroot.'/file.php/'.$path.'/thumb/'.$src : $source; } else { $source = $CFG->wwwroot.'/file.php?file=/'.$path.'/'.$src; $thumbnailsource = file_exists($thumbnaillocation) ? $CFG->wwwroot.'/file.php?file=/'.$path.'/thumb/'.$src : $source; } if ($template == 'listtemplate') { $width = $this->field->param4 ? ' width="'.s($this->field->param4).'" ' : ' '; $height = $this->field->param5 ? ' height="'.s($this->field->param5).'" ' : ' '; $str = ''.s($alt).''; } else { $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; $str = ''.s($alt).''; } return $str; } return false; } function update_field() { // Get the old field data so that we can check whether the thumbnail dimensions have changed $oldfield = get_record('data_fields', 'id', $this->field->id); if (!update_record('data_fields', $this->field)) { notify('updating of new field failed!'); return false; } // Have the thumbnail dimensions changed? if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { // Check through all existing records and update the thumbnail if ($contents = get_records('data_content', 'fieldid', $this->field->id)) { if (count($contents) > 20) { notify(get_string('resizingimages', 'data'), 'notifysuccess'); echo "\n\n"; // To make sure that ob_flush() has the desired effect ob_flush(); } foreach ($contents as $content) { @set_time_limit(300); // Might be slow! $this->update_thumbnail($content); } } } return true; } function update_content($recordid, $value, $name) { parent::update_content($recordid, $value, $name); $content = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid); $this->update_thumbnail($content); // Regenerate the thumbnail } /** * (Re)generate thumbnail image according to the dimensions specified in the field settings. * If thumbnail width and height are BOTH not specified then no thumbnail is generated, and * additionally an attempted delete of the existing thumbnail takes place. */ function update_thumbnail($content) { global $CFG; require_once($CFG->libdir . '/gdlib.php'); $datalocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'. $this->data->id.'/'.$this->field->id.'/'.$content->recordid; $originalfile = $datalocation.'/'.$content->content; if (!file_exists($originalfile)) { return; } if (!file_exists($datalocation.'/thumb')) { mkdir($datalocation.'/thumb', 0777); } $thumbnaillocation = $datalocation.'/thumb/'.$content->content; $imageinfo = GetImageSize($originalfile); $image->width = $imageinfo[0]; $image->height = $imageinfo[1]; $image->type = $imageinfo[2]; if (!$image->width || !$image->height) { // Should not happen return; } switch ($image->type) { case 1: if (function_exists('ImageCreateFromGIF')) { $im = ImageCreateFromGIF($originalfile); } else { return; } break; case 2: if (function_exists('ImageCreateFromJPEG')) { $im = ImageCreateFromJPEG($originalfile); } else { return; } break; case 3: if (function_exists('ImageCreateFromPNG')) { $im = ImageCreateFromPNG($originalfile); } else { return; } break; } // fix for MDL-7270 $thumbwidth = isset($this->field->param4)?$this->field->param4:''; $thumbheight = isset($this->field->param5)?$this->field->param5:''; if ($thumbwidth || $thumbheight) { // Only if either width OR height specified do we want a thumbnail $wcrop = $image->width; $hcrop = $image->height; if ($thumbwidth && !$thumbheight) { $thumbheight = $image->height * $thumbwidth / $image->width; } else if($thumbheight && !$thumbwidth) { $thumbheight = $image->width * $thumbheight / $image->height; } else { // BOTH are set - may need to crop if aspect ratio differs $hratio = $image->height / $thumbheight; $wratio = $image->width / $thumbwidth; if ($wratio > $hratio) { // Crop the width $wcrop = intval($thumbwidth * $hratio); } elseif($hratio > $wratio) { // Crop the height $hcrop = intval($thumbheight * $wratio); } } // At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { $im1 = ImageCreateTrueColor($thumbwidth,$thumbheight); } else { $im1 = ImageCreate($thumbwidth,$thumbheight); } $cx = $image->width / 2; $cy = $image->height / 2; // These "half" measurements use the "crop" values rather than the actual dimensions $halfwidth = floor($wcrop * 0.5); $halfheight = floor($hcrop * 0.5); ImageCopyBicubic($im1, $im, 0, 0, $cx-$halfwidth, $cy-$halfheight, $thumbwidth, $thumbheight, $halfwidth*2, $halfheight*2); if (function_exists('ImageJpeg')) { @touch($thumbnaillocation); // Helps in Safe mode if (ImageJpeg($im1, $thumbnaillocation, 90)) { @chmod($thumbnaillocation, 0666); } } } else { // Try and remove the thumbnail - we don't want thumbnailing active @unlink($thumbnaillocation); } } } ?>