00001 <?php
00002
00003
00004
00005
00006
00007
00008 class ImageToolsGD implements IImageTools {
00009
00010
00011
00012
00013
00014 public function create_from_file($file) {
00015 $ret = false;
00016 $imageinfo = getimagesize($file);
00017 if ($imageinfo !== false) {
00018 $handle = false;
00019 switch($imageinfo[2]) {
00020 case IMAGETYPE_JPEG:
00021 $handle = imagecreatefromjpeg($file);
00022 break;
00023 case IMAGETYPE_PNG:
00024 $handle = imagecreatefrompng($file);
00025 break;
00026 case IMAGETYPE_GIF:
00027 $handle = imagecreatefromgif($file);
00028 break;
00029 }
00030 if ($handle) {
00031 $ret = new ImageInformationGD($handle, $imageinfo[2]);
00032 }
00033 }
00034 return $ret;
00035 }
00036
00037
00038
00039
00040
00041
00042 public function create_from_binary_data($data) {
00043 $ret = false;
00044 $tmp = tempnam(Config::get_value(Config::TEMP_DIR), 'imgi');
00045 if ($tmp) {
00046 if (file_put_contents($tmp, $data) !== false) {
00047 $ret = $this->create_from_file($tmp);
00048 }
00049 }
00050 unlink($tmp);
00051 return $ret;
00052 }
00053
00054
00055
00056
00057
00058
00059 public function resize(IImageInformation $src, $width, $height) {
00060 $handle = imagecreatetruecolor($width, $height);
00061 imagecopyresampled($handle, $src->handle, 0, 0, 0, 0, $width, $height, $src->get_width(), $src->get_height());
00062 return new ImageInformationGD($handle, $src->type);
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 public function resize_fit(IImageInformation $src, $width, $height) {
00073 Load::components('imagetoolscalculator');
00074 $rect = ImageToolsCalculator::fit($src->get_width(), $src->get_height(), $width, $height);
00075 $handle = imagecreatetruecolor($rect->width, $rect->height);
00076 imagecopyresampled($handle, $src->handle, 0, 0, 0, 0, $rect->width, $rect->height, $src->get_width(), $src->get_height());
00077 return new ImageInformationGD($handle, $src->type);
00078 }
00079
00080
00081
00082
00083
00084
00085 public function crop(IImageInformation $src, $x, $y, $width, $height) {
00086 $handle = imagecreatetruecolor($width, $height);
00087 imagecopy($handle, $src->handle, 0, 0, $x, $y, $width, $height);
00088 return new ImageInformationGD($handle, $src->type);
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 public function fit(IImageInformation $src, $width, $height, $backgroundcolor = 0xFFFFFF) {
00099 $handle = imagecreatetruecolor($width, $height);
00100 imagefill($handle, 0, 0, $backgroundcolor);
00101
00102 Load::components('imagetoolscalculator');
00103 $rect = ImageToolsCalculator::fit($src->get_width(), $src->get_height(), $width, $height);
00104 $rect = ImageToolsCalculator::center($rect->width, $rect->height, $width, $height);
00105
00106 imagecopyresampled($handle, $src->handle, $rect->x, $rect->y, 0, 0, $rect->width, $rect->height, $src->get_width(), $src->get_height());
00107 return new ImageInformationGD($handle, $src->type);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 public function watermark(IImageInformation $src, $text = false) {
00119 if (empty($text)) {
00120 $text = '© ' . Config::get_value(Config::TITLE);
00121 }
00122 $w = $src->get_width();
00123 $h = $src->get_height();
00124 $size = 40;
00125 $font = Load::get_module_dir('lib.imagetools'). '3rdparty/arial.ttf';
00126 $watermark = imagecreatetruecolor($w, $h);
00127 imagecopy($watermark, $src->handle, 0, 0, 0, 0, $w, $h);
00128 $color = imagecolorallocatealpha($watermark, 0xFF, 0xFF, 0xFF, 0x70);
00129 if ($w >= $h) {
00130
00131 imagettftext($watermark, $size, 0, 3, $h - $sie - 3, $color, $font, $text);
00132 }
00133 else {
00134
00135 imagettftext($watermark, $size, 90, $w - $size - 3, $h - 3, $color, $font, $text);
00136 }
00137 return new ImageInformationGD($watermark, $src->type);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 public function rotate(IImageInformation $src, $degrees, $backgroundcolor = 0xFFFFFF) {
00149 $rotated = imagerotate($src->handle, $degrees, $backgroundcolor);
00150 return new ImageInformationGD($rotated, $src->type);
00151 }
00152 }
00153
00154
00155
00156
00157 class ImageInformationGD implements IImageInformation {
00158 public $handle;
00159 public $type;
00160
00161 public function __construct($handle, $imagetype) {
00162 $this->handle = $handle;
00163 $this->type = $imagetype;
00164 }
00165
00166 public function __destruct() {
00167 @imagedestroy($this->handle);
00168 }
00169
00170
00171
00172
00173
00174 public function get_height() {
00175 return imagesy($this->handle);
00176 }
00177
00178
00179
00180
00181
00182 public function get_width() {
00183 return imagesx($this->handle);
00184 }
00185
00186
00187
00188
00189
00190 public function get_binary_data() {
00191 $ret = false;
00192 $tmp = tempnam(Config::get_value(Config::TEMP_DIR), 'imgo');
00193 if ($this->save_to_file($tmp, false)) {
00194 $ret = file_get_contents($tmp);
00195 }
00196 unlink($tmp);
00197 return $ret;
00198 }
00199
00200
00201
00202
00203
00204 public function save_to_file($file, $add_extension = true) {
00205 $ret = false;
00206 if ($add_extension) {
00207 $file .= '.' . $this->get_extension();
00208 }
00209 switch ($this->type) {
00210 case IMAGETYPE_JPEG:
00211 $ret = imagejpeg($this->handle, $file);
00212 break;
00213 case IMAGETYPE_PNG:
00214 $ret = imagepng($this->handle, $file);
00215 break;
00216 case IMAGETYPE_GIF:
00217 $ret = imagegif($this->handle, $file);
00218 break;
00219 }
00220 return $ret;
00221 }
00222
00223
00224
00225
00226
00227 public function get_mime_type() {
00228 return image_type_to_mime_type($this->type);
00229 }
00230
00231
00232
00233
00234
00235 public function get_extension() {
00236 switch($this->type) {
00237 case IMAGETYPE_JPEG:
00238 return 'jpg';
00239 case IMAGETYPE_PNG:
00240 return 'png';
00241 case IMAGETYPE_GIF:
00242 return 'gif';
00243 }
00244 }
00245 }