3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
22 * @subpackage flickr_public
23 * @author Dongsheng Cai <dongsheng@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
34 function __construct($img) {
35 ini_set('gd.jpeg_ignore_warning', 1);
36 if(!function_exists('imagecreatefrompng')
37 and !function_exists('imagecreatefromjpeg')) {
38 throw new moodle_exception('gdnotexist');
40 if(!file_exists($img) or !is_readable($img)) {
41 throw new moodle_exception('invalidfile');
44 $this->imagepath
= $img;
46 $this->info
= getimagesize($this->imagepath
);
48 switch($this->info
['mime']) {
50 $this->image
= imagecreatefromjpeg($this->imagepath
);
53 $this->image
= imagecreatefrompng($this->imagepath
);
56 $this->image
= imagecreatefromgif($this->imagepath
);
61 $this->width
= imagesx($this->image
);
62 $this->height
= imagesy($this->image
);
66 imagedestroy($this->image
);
67 imagedestroy($this->backup
);
72 $this->image
= $this->backup
;
76 function watermark($text='', $pos=array(), $options=array()) {
78 $text = iconv('ISO-8859-8', 'UTF-8', $text);
79 if (empty($options['fontsize'])) {
80 if (!empty($options['ttf'])) {
81 $options['fontsize'] = 12;
83 $options['fontsize'] = 1;
87 if (empty($options['font'])) {
88 $options['font'] = $CFG->libdir
. '/default.ttf';
90 if (empty($options['angle'])) {
91 $options['angle'] = 0;
93 $clr = imagecolorallocate($this->image
, 255, 255, 255);
94 if (!empty($options['ttf'])) {
95 imagettftext($this->image
,
96 $options['fontsize'], // font size
99 $pos[1]+
$options['fontsize'],
104 imagestring($this->image
, $options['fontsize'], $pos[0], $pos[1], $text, $clr);
109 function rotate($angle=0, $bgcolor=0) {
110 $this->image
= imagerotate($this->image
, $angle, $bgcolor);
114 function resize($w, $h, $use_resize = true) {
115 if(empty($h) && !empty($w)) {
116 $h = $this->height
* ($w/$this->width
);
118 if(!empty($h) && empty($w)) {
119 $w = $this->width
* ($h/$this->height
);
121 $new_img = imagecreatetruecolor($w, $h);
122 imagealphablending($new_img, false);
123 imagecopyresampled($new_img /* dst */, $this->image
/* src */, 0, 0, 0, 0, $w, $h, $this->width
, $this->height
);
124 $this->image
= $new_img;
128 function saveas($imagepath) {
129 switch($this->info
['mime']) {
131 return imagejpeg($this->image
, $imagepath);
134 return imagepng($this->image
, $imagepath);
137 return imagegif($this->image
, $imagepath);
142 if(!$this->destroy()) {
150 header('Content-type: '.$this->info
['mime']);
151 switch($this->info
['mime']) {
153 imagepng($this->image
);
156 imagejpeg($this->image
);
159 imagegif($this->image
);