00001 <?php
00013 define('IMAGE_FLIP_HORIZONTAL', 1);
00014 define('IMAGE_FLIP_VERTICAL', 2);
00015 define('IMAGE_FLIP_BOTH', 3);
00016
00017 class ppImage extends Plugin
00018 {
00019 function ppImage()
00020 {
00021 $this->Plugin();
00022 }
00023
00027 function convert($srcType, $srcFile, $dstFile)
00028 {
00029 $size = getimagesize($srcFile);
00030
00031 return $this->resize($srcType, $srcFile, $dstFile, $size[0], $size[1], true);
00032 }
00033
00042 function rotate($srcType, $srcFile, $dstFile, $angle)
00043 {
00044 $src = $this->_load_image($srcFile, $srcType);
00045 if(!$src) return false;
00046
00047 $dst = imagerotate($src, $angle, 0);
00048 if(!$dst) return false;
00049
00050 if(!$this->_create_image($dst, $dstFile, $srcType)) return false;
00051
00052 imagedestroy($src);
00053 imagedestroy($dst);
00054
00055 return true;
00056 }
00057
00067 function auto_rotate($srcType, $srcFile, $dstFile, $angle)
00068 {
00069 if(!extension_loaded('exif')) {
00070 trigger_error("ppImage::auto_rotate() requires the exif PHP extension.");
00071 return false;
00072 }
00073
00074 $exif = exif_read_data($srcFile);
00075 if($exif == false) return false;
00076
00077 if(!isset($exif['IFD0']['Orientation'])) return false;
00078
00079 $rotate = 0;
00080 $flip = false;
00081 switch($o) {
00082 case 1:
00083 $rotate = 0;
00084 $flip = false;
00085 break;
00086 case 2:
00087 $rotate = 0;
00088 $flip = true;
00089 break;
00090 case 3:
00091 $rotate = 180;
00092 $flip = false;
00093 break;
00094 case 4:
00095 $rotate = 180;
00096 $flip = true;
00097 break;
00098 case 5:
00099 $rotate = 90;
00100 $flip = true;
00101 break;
00102 case 6:
00103 $rotate = 90;
00104 $flip = false;
00105 break;
00106 case 7:
00107 $rotate = 270;
00108 $flip = true;
00109 break;
00110 case 8:
00111 $rotate = 270;
00112 $flip = false;
00113 break;
00114 }
00115 if($flip) {
00116 $this->flip($srcType, $srcFile, $srcFile, IMAGE_FLIP_HORIZONTAL);
00117 }
00118 if($rotate) {
00119 $this->rotate($srcType, $srcFile, $srcFile, $rotate);
00120 }
00121 if($srcFile != $dstFile) {
00122 copy($srcFile, $dstFile);
00123 }
00124
00125 return true;
00126 }
00127
00139 function resize($srcType, $srcFile, $dstFile, $max_width=0, $max_height=0, $maintain_aspect=true)
00140 {
00141 $size = getimagesize($srcFile);
00142
00143 $width = $size[0];
00144 $height = $size[1];
00145
00146 if(!$max_width) $max_width = $width;
00147 if(!$max_height) $max_height = $height;
00148
00149 if($maintain_aspect) {
00150
00151 $x_ratio = $max_width / $width;
00152 $y_ratio = $max_height / $height;
00153
00154 if(($width <= $max_width) && ($height <= $max_height)) {
00155 $tn_width = $width;
00156 $tn_height = $height;
00157 } else if(($x_ratio * $height) < $max_height) {
00158 $tn_height = ceil($x_ratio * $height);
00159 $tn_width = $max_width;
00160 } else {
00161 $tn_width = ceil($y_ratio * $width);
00162 $tn_height = $max_height;
00163 }
00164 } else {
00165 $tn_width = $max_width;
00166 $tn_height = $max_height;
00167 }
00168
00169
00170 $src = $this->_load_image($srcFile, $srcType);
00171 if(!$src) return false;
00172
00173 $dst = imagecreatetruecolor($tn_width, $tn_height);
00174 imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
00175
00176
00177 $this->_create_image($dst, $dstFile, 'image/jpeg');
00178
00179 imagedestroy($src);
00180 imagedestroy($dst);
00181
00182 return true;
00183 }
00184
00194 function flip($srcType, $srcType, $dstFile, $mode)
00195 {
00196 $size = getimagesize($srcFile);
00197
00198 $width = $size[0];
00199 $height = $size[1];
00200
00201 $src_x = 0;
00202 $src_y = 0;
00203 $src_width = $width;
00204 $src_height = $height;
00205
00206 switch((int)$mode) {
00207 case IMAGE_FLIP_HORIZONTAL:
00208 $src_y = $height;
00209 $src_height = -$height;
00210 break;
00211 case IMAGE_FLIP_VERTICAL:
00212 $src_x = $width;
00213 $src_width = -$width;
00214 break;
00215 case IMAGE_FLIP_BOTH:
00216 $src_x = $width;
00217 $src_y = $height;
00218 $src_width = -$width;
00219 $src_height = -$height;
00220 break;
00221 default:
00222 return false;
00223 }
00224
00225 $src = $this->_load_image($srcFile, $srcType);
00226 if(!$src) return false;
00227
00228 $dest = imagecreatetruecolor($width, $height);
00229
00230 if(imagecopyresampled($dest, $src, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) {
00231
00232 $this->_create_image($dest, $dstFile, 'image/jpeg');
00233 return true;
00234 }
00235
00236 return false;
00237 }
00238
00239 function _load_image($fn, $mime)
00240 {
00241 switch($mime) {
00242 case 'image/gif': return ImageCreateFromGif($fn);
00243 case 'image/jpeg':
00244 case 'image/pjpeg': return ImageCreateFromJpeg($fn);
00245 case 'image/png': return ImageCreateFromPng($fn);
00246 }
00247 return false;
00248 }
00249
00250 function _create_image($img, $fn, $mime)
00251 {
00252 $q = defined('JPEG_QUALITY') ? JPEG_QUALITY : 75;
00253
00254 switch($mime) {
00255 case 'image/gif': return ImageGif($img, $fn);
00256 case 'image/jpeg':
00257 case 'image/pjpeg': return ImageJpeg($img, $fn, $q);
00258 case 'image/png': return ImagePng($img, $fn);
00259 }
00260 return false;
00261 }
00262
00263 }
00264
00265 ?>