|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 class moodle_image { 00027 private $imagepath; 00028 private $info; 00029 private $width; 00030 private $height; 00031 private $image; 00032 private $backup; 00033 00034 function __construct($img) { 00035 ini_set('gd.jpeg_ignore_warning', 1); 00036 if(!function_exists('imagecreatefrompng') 00037 and !function_exists('imagecreatefromjpeg')) { 00038 throw new moodle_exception('gdnotexist'); 00039 } 00040 if(!file_exists($img) or !is_readable($img)) { 00041 throw new moodle_exception('invalidfile'); 00042 } 00043 00044 $this->imagepath = $img; 00045 unset($img); 00046 $this->info = getimagesize($this->imagepath); 00047 00048 switch($this->info['mime']) { 00049 case 'image/jpeg': 00050 $this->image = imagecreatefromjpeg($this->imagepath); 00051 break; 00052 case 'image/png': 00053 $this->image = imagecreatefrompng($this->imagepath); 00054 break; 00055 case 'image/gif': 00056 $this->image = imagecreatefromgif($this->imagepath); 00057 break; 00058 default: 00059 break; 00060 } 00061 $this->width = imagesx($this->image); 00062 $this->height = imagesy($this->image); 00063 } 00064 00065 function destroy() { 00066 imagedestroy($this->image); 00067 imagedestroy($this->backup); 00068 return true; 00069 } 00070 00071 function undo() { 00072 $this->image = $this->backup; 00073 return $this; 00074 } 00075 00076 function watermark($text='', $pos=array(), $options=array()) { 00077 global $CFG; 00078 $text = iconv('ISO-8859-8', 'UTF-8', $text); 00079 if (empty($options['fontsize'])) { 00080 if (!empty($options['ttf'])) { 00081 $options['fontsize'] = 12; 00082 } else { 00083 $options['fontsize'] = 1; 00084 } 00085 } 00086 00087 if (empty($options['font'])) { 00088 $options['font'] = $CFG->libdir . '/default.ttf'; 00089 } 00090 if (empty($options['angle'])) { 00091 $options['angle'] = 0; 00092 } 00093 $clr = imagecolorallocate($this->image, 255, 255, 255); 00094 if (!empty($options['ttf'])) { 00095 imagettftext($this->image, 00096 $options['fontsize'], // font size 00097 $options['angle'], 00098 $pos[0], 00099 $pos[1]+$options['fontsize'], 00100 $clr, 00101 $options['font'], 00102 $text); 00103 } else { 00104 imagestring($this->image, $options['fontsize'], $pos[0], $pos[1], $text, $clr); 00105 } 00106 return $this; 00107 } 00108 00109 function rotate($angle=0, $bgcolor=0) { 00110 $this->image = imagerotate($this->image, $angle, $bgcolor); 00111 return $this; 00112 } 00113 00114 function resize($w, $h, $use_resize = true) { 00115 if(empty($h) && !empty($w)) { 00116 $h = $this->height * ($w/$this->width); 00117 } 00118 if(!empty($h) && empty($w)) { 00119 $w = $this->width * ($h/$this->height); 00120 } 00121 $new_img = imagecreatetruecolor($w, $h); 00122 imagealphablending($new_img, false); 00123 imagecopyresampled($new_img /* dst */, $this->image /* src */, 0, 0, 0, 0, $w, $h, $this->width, $this->height); 00124 $this->image = $new_img; 00125 return $this; 00126 } 00127 00128 function saveas($imagepath) { 00129 switch($this->info['mime']) { 00130 case 'image/jpeg': 00131 return imagejpeg($this->image, $imagepath); 00132 break; 00133 case 'image/png': 00134 return imagepng($this->image, $imagepath); 00135 break; 00136 case 'image/gif': 00137 return imagegif($this->image, $imagepath); 00138 break; 00139 default: 00140 break; 00141 } 00142 if(!$this->destroy()) { 00143 return false; 00144 } else { 00145 return $this; 00146 } 00147 } 00148 00149 function display() { 00150 header('Content-type: '.$this->info['mime']); 00151 switch($this->info['mime']) { 00152 case 'image/png': 00153 imagepng($this->image); 00154 break; 00155 case 'image/jpeg': 00156 imagejpeg($this->image); 00157 break; 00158 case 'image/gif': 00159 imagegif($this->image); 00160 break; 00161 default: 00162 break; 00163 } 00164 $this->destroy(); 00165 return $this; 00166 } 00167 } 00168