2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once($CFG->dirroot
.'/mod/data/field/file/field.class.php'); // Base class is 'file'
27 class data_field_picture
extends data_field_file
{
29 var $type = 'picture';
31 var $previewwidth = 50;
32 var $previewheight = 50;
34 function data_field_picture($field=0, $data=0) {
35 parent
::data_field_base($field, $data);
38 function display_add_field($recordid=0){
46 if ($content = get_record('data_content', 'fieldid', $this->field
->id
, 'recordid', $recordid)) {
47 $filename = $content->content
;
48 $description = $content->content1
;
51 $path = $this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid;
53 if ($CFG->slasharguments
) {
54 $filepath = $CFG->wwwroot
.'/file.php/'.$path.'/'.$filename;
56 $filepath = $CFG->wwwroot
.'/file.php?file=/'.$path.'/'.$filename;
60 $str = '<div title="'.s($this->field
->description
).'">';
61 $str .= '<fieldset><legend><span class="accesshide">'.$this->field
->name
.'</span></legend>';
62 $str .= '<input type="hidden" name ="field_'.$this->field
->id
.'_file" id="field_'.$this->field
->id
.'_file" value="fakevalue" />';
63 $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 />';
64 $str .= '<label for="field_'.$this->field
->id
.'_filename">'.get_string('alttext','data') .'</label> <input type="text" name="field_'
65 .$this->field
->id
.'_filename" id="field_'.$this->field
->id
.'_filename" value="'.s($description).'" /><br />';
66 $str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field
->param3
).'" />';
68 $str .= '<img width="'.s($this->previewwidth
).'" height="'.s($this->previewheight
).'" src="'.$filepath.'" alt="" />';
70 $str .= '</fieldset>';
75 function display_search_field($value = '') {
76 return '<input type="text" size="16" name="f_'.$this->field
->id
.'" value="'.$value.'" />';
79 function parse_search_field() {
80 return optional_param('f_'.$this->field
->id
, '', PARAM_NOTAGS
);
83 function generate_sql($tablealias, $value) {
84 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') ";
87 function display_browse_field($recordid, $template) {
90 if ($content = get_record('data_content', 'fieldid', $this->field
->id
, 'recordid', $recordid)){
91 if (isset($content->content
)){
92 $contents[0] = $content->content
;
93 $contents[1] = $content->content1
;
96 if (empty($contents[0])) { // Nothing to show
100 $alt = empty($contents[1])?
'':$contents[1];
101 $title = empty($contents[1])?
'':$contents[1];
104 $path = $this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid;
105 $thumbnaillocation = $CFG->dataroot
.'/'.$this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid.'/thumb/'.$src;
107 if ($CFG->slasharguments
) {
108 $source = $CFG->wwwroot
.'/file.php/'.$path.'/'.$src;
109 $thumbnailsource = file_exists($thumbnaillocation) ?
$CFG->wwwroot
.'/file.php/'.$path.'/thumb/'.$src : $source;
111 $source = $CFG->wwwroot
.'/file.php?file=/'.$path.'/'.$src;
112 $thumbnailsource = file_exists($thumbnaillocation) ?
$CFG->wwwroot
.'/file.php?file=/'.$path.'/thumb/'.$src : $source;
115 if ($template == 'listtemplate') {
116 $width = $this->field
->param4 ?
' width="'.s($this->field
->param4
).'" ' : ' ';
117 $height = $this->field
->param5 ?
' height="'.s($this->field
->param5
).'" ' : ' ';
118 $str = '<a href="view.php?d='.$this->field
->dataid
.'&rid='.$recordid.'"><img '.
119 $width.$height.' src="'.$thumbnailsource.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>';
121 $width = $this->field
->param1 ?
' width="'.s($this->field
->param1
).'" ':' ';
122 $height = $this->field
->param2 ?
' height="'.s($this->field
->param2
).'" ':' ';
123 $str = '<a href="'.$source.'"><img '.$width.$height.' src="'.$source.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>';
130 function update_field() {
132 // Get the old field data so that we can check whether the thumbnail dimensions have changed
133 $oldfield = get_record('data_fields', 'id', $this->field
->id
);
135 if (!update_record('data_fields', $this->field
)) {
136 notify('updating of new field failed!');
140 // Have the thumbnail dimensions changed?
141 if ($oldfield && ($oldfield->param4
!= $this->field
->param4 ||
$oldfield->param5
!= $this->field
->param5
)) {
143 // Check through all existing records and update the thumbnail
144 if ($contents = get_records('data_content', 'fieldid', $this->field
->id
)) {
145 if (count($contents) > 20) {
146 notify(get_string('resizingimages', 'data'), 'notifysuccess');
147 echo "\n\n"; // To make sure that ob_flush() has the desired effect
150 foreach ($contents as $content) {
151 @set_time_limit
(300); // Might be slow!
152 $this->update_thumbnail($content);
159 function update_content($recordid, $value, $name) {
160 parent
::update_content($recordid, $value, $name);
162 $content = get_record('data_content','fieldid', $this->field
->id
, 'recordid', $recordid);
164 $this->update_thumbnail($content); // Regenerate the thumbnail
168 * (Re)generate thumbnail image according to the dimensions specified in the field settings.
169 * If thumbnail width and height are BOTH not specified then no thumbnail is generated, and
170 * additionally an attempted delete of the existing thumbnail takes place.
172 function update_thumbnail($content) {
175 require_once($CFG->libdir
. '/gdlib.php');
177 $datalocation = $CFG->dataroot
.'/'.$this->data
->course
.'/'.$CFG->moddata
.'/data/'.
178 $this->data
->id
.'/'.$this->field
->id
.'/'.$content->recordid
;
179 $originalfile = $datalocation.'/'.$content->content
;
180 if (!file_exists($originalfile)) {
183 if (!file_exists($datalocation.'/thumb')) {
184 mkdir($datalocation.'/thumb', 0777);
186 $thumbnaillocation = $datalocation.'/thumb/'.$content->content
;
187 $imageinfo = GetImageSize($originalfile);
188 $image->width
= $imageinfo[0];
189 $image->height
= $imageinfo[1];
190 $image->type
= $imageinfo[2];
192 if (!$image->width ||
!$image->height
) { // Should not happen
196 switch ($image->type
) {
198 if (function_exists('ImageCreateFromGIF')) {
199 $im = ImageCreateFromGIF($originalfile);
205 if (function_exists('ImageCreateFromJPEG')) {
206 $im = ImageCreateFromJPEG($originalfile);
212 if (function_exists('ImageCreateFromPNG')) {
213 $im = ImageCreateFromPNG($originalfile);
221 $thumbwidth = isset($this->field
->param4
)?
$this->field
->param4
:'';
222 $thumbheight = isset($this->field
->param5
)?
$this->field
->param5
:'';
224 if ($thumbwidth ||
$thumbheight) { // Only if either width OR height specified do we want a thumbnail
226 $wcrop = $image->width
;
227 $hcrop = $image->height
;
229 if ($thumbwidth && !$thumbheight) {
230 $thumbheight = $image->height
* $thumbwidth / $image->width
;
231 } else if($thumbheight && !$thumbwidth) {
232 $thumbwidth = $image->width
* $thumbheight / $image->height
;
233 } else { // BOTH are set - may need to crop if aspect ratio differs
234 $hratio = $image->height
/ $thumbheight;
235 $wratio = $image->width
/ $thumbwidth;
236 if ($wratio > $hratio) { // Crop the width
237 $wcrop = intval($thumbwidth * $hratio);
238 } elseif($hratio > $wratio) { // Crop the height
239 $hcrop = intval($thumbheight * $wratio);
243 // At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop
244 if (function_exists('ImageCreateTrueColor') and $CFG->gdversion
>= 2) {
245 $im1 = ImageCreateTrueColor($thumbwidth,$thumbheight);
247 $im1 = ImageCreate($thumbwidth,$thumbheight);
250 // Prevent alpha blending for PNG images
251 if ($image->type
== 3) {
252 imagealphablending($im1, false);
255 $cx = $image->width
/ 2;
256 $cy = $image->height
/ 2;
258 // These "half" measurements use the "crop" values rather than the actual dimensions
259 $halfwidth = floor($wcrop * 0.5);
260 $halfheight = floor($hcrop * 0.5);
262 ImageCopyBicubic($im1, $im, 0, 0, $cx-$halfwidth, $cy-$halfheight,
263 $thumbwidth, $thumbheight, $halfwidth*2, $halfheight*2);
265 // Save alpha transparency for PNG images
266 if ($image->type
== 3) {
267 imagesavealpha($im1, true);
270 if (function_exists('ImageJpeg') && $image->type
!= 3) {
271 @touch
($thumbnaillocation); // Helps in Safe mode
272 if (ImageJpeg($im1, $thumbnaillocation, 90)) {
273 @chmod
($thumbnaillocation, 0666);
275 } elseif (function_exists('ImagePng') && $image->type
== 3) {
276 @touch
($thumbnaillocation); // Helps in Safe mode
277 if (ImagePng($im1, $thumbnaillocation, 9)) {
278 @chmod
($thumbnaillocation, 0666);
282 } else { // Try and remove the thumbnail - we don't want thumbnailing active
283 @unlink
($thumbnaillocation);