3 if(! class_exists("Photo")) {
14 * supported mimetypes and corresponding file extensions
16 static function supportedTypes() {
18 $t['image/jpeg'] ='jpg';
19 if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
23 public function __construct($data, $type="image/jpeg") {
25 $this->types = $this->supportedTypes();
26 if (!array_key_exists($type,$this->types)){
31 $this->image = @imagecreatefromstring($data);
32 if($this->image !== FALSE) {
33 $this->width = imagesx($this->image);
34 $this->height = imagesy($this->image);
36 imagealphablending($this->image, false);
37 imagesavealpha($this->image, true);
41 public function __destruct() {
43 imagedestroy($this->image);
46 public function is_valid() {
50 public function getWidth() {
54 public function getHeight() {
58 public function getImage() {
62 public function getType() {
65 public function getExt() {
66 return $this->types[$this->type];
69 public function scaleImage($max) {
71 $width = $this->width;
72 $height = $this->height;
74 $dest_width = $dest_height = 0;
76 if((! $width)|| (! $height))
79 if($width > $max && $height > $max) {
80 if($width > $height) {
82 $dest_height = intval(( $height * $max ) / $width);
85 $dest_width = intval(( $width * $max ) / $height);
92 $dest_height = intval(( $height * $max ) / $width);
95 if( $height > $max ) {
96 $dest_width = intval(( $width * $max ) / $height);
100 $dest_width = $width;
101 $dest_height = $height;
107 $dest = imagecreatetruecolor( $dest_width, $dest_height );
108 imagealphablending($dest, false);
109 imagesavealpha($dest, true);
110 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
111 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
113 imagedestroy($this->image);
114 $this->image = $dest;
115 $this->width = imagesx($this->image);
116 $this->height = imagesy($this->image);
120 public function rotate($degrees) {
121 $this->image = imagerotate($this->image,$degrees,0);
122 $this->width = imagesx($this->image);
123 $this->height = imagesy($this->image);
126 public function flip($horiz = true, $vert = false) {
127 $w = imagesx($this->image);
128 $h = imagesy($this->image);
129 $flipped = imagecreate($w, $h);
131 for ($x = 0; $x < $w; $x++) {
132 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
136 for ($y = 0; $y < $h; $y++) {
137 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
140 $this->image = $flipped;
143 public function orient($filename) {
144 // based off comment on http://php.net/manual/en/function.imagerotate.php
146 if(! function_exists('exif_read_data'))
149 $exif = exif_read_data($filename);
150 $ort = $exif['Orientation'];
157 case 2: // horizontal flip
161 case 3: // 180 rotate left
165 case 4: // vertical flip
166 $this->flip(false, true);
169 case 5: // vertical flip + 90 rotate right
170 $this->flip(false, true);
174 case 6: // 90 rotate right
178 case 7: // horizontal flip + 90 rotate right
183 case 8: // 90 rotate left
191 public function scaleImageUp($min) {
193 $width = $this->width;
194 $height = $this->height;
196 $dest_width = $dest_height = 0;
198 if((! $width)|| (! $height))
201 if($width < $min && $height < $min) {
202 if($width > $height) {
204 $dest_height = intval(( $height * $min ) / $width);
207 $dest_width = intval(( $width * $min ) / $height);
212 if( $width < $min ) {
214 $dest_height = intval(( $height * $min ) / $width);
217 if( $height < $min ) {
218 $dest_width = intval(( $width * $min ) / $height);
222 $dest_width = $width;
223 $dest_height = $height;
229 $dest = imagecreatetruecolor( $dest_width, $dest_height );
230 imagealphablending($dest, false);
231 imagesavealpha($dest, true);
232 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
233 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
235 imagedestroy($this->image);
236 $this->image = $dest;
237 $this->width = imagesx($this->image);
238 $this->height = imagesy($this->image);
244 public function scaleImageSquare($dim) {
246 $dest = imagecreatetruecolor( $dim, $dim );
247 imagealphablending($dest, false);
248 imagesavealpha($dest, true);
249 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
250 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
252 imagedestroy($this->image);
253 $this->image = $dest;
254 $this->width = imagesx($this->image);
255 $this->height = imagesy($this->image);
259 public function cropImage($max,$x,$y,$w,$h) {
260 $dest = imagecreatetruecolor( $max, $max );
261 imagealphablending($dest, false);
262 imagesavealpha($dest, true);
263 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
264 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
266 imagedestroy($this->image);
267 $this->image = $dest;
268 $this->width = imagesx($this->image);
269 $this->height = imagesy($this->image);
272 public function saveImage($path) {
275 $quality = get_config('system','png_quality');
276 if((! $quality) || ($quality > 9))
277 $quality = PNG_QUALITY;
278 imagepng($this->image, $path, $quality);
281 $quality = get_config('system','jpeg_quality');
282 if((! $quality) || ($quality > 100))
283 $quality = JPEG_QUALITY;
284 imagejpeg($this->image,$path,$quality);
289 public function imageString() {
293 $quality = get_config('system','png_quality');
294 if((! $quality) || ($quality > 9))
295 $quality = PNG_QUALITY;
296 imagepng($this->image,NULL, $quality);
299 $quality = get_config('system','jpeg_quality');
300 if((! $quality) || ($quality > 100))
301 $quality = JPEG_QUALITY;
303 imagejpeg($this->image,NULL,$quality);
305 $s = ob_get_contents();
312 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
314 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
318 $guid = $r[0]['guid'];
322 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
329 $r = q("UPDATE `photo`
333 `resource-id` = '%s',
348 where id = %d limit 1",
354 dbesc(datetime_convert()),
355 dbesc(datetime_convert()),
356 dbesc(basename($filename)),
359 intval($this->height),
360 intval($this->width),
361 dbesc($this->imageString()),
372 $r = q("INSERT INTO `photo`
373 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
374 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
379 dbesc(datetime_convert()),
380 dbesc(datetime_convert()),
381 dbesc(basename($filename)),
384 intval($this->height),
385 intval($this->width),
386 dbesc($this->imageString()),
401 * Guess image mimetype from filename or from Content-Type header
403 * @arg $filename string Image filename
404 * @arg $fromcurl boolean Check Content-Type header from curl request
406 function guess_image_type($filename, $fromcurl=false) {
407 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
412 $h = explode("\n",$a->get_curl_headers());
414 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
417 if (array_key_exists('Content-Type', $headers))
418 $type = $headers['Content-Type'];
421 $ext = pathinfo($filename, PATHINFO_EXTENSION);
422 $types = Photo::supportedTypes();
423 $type = "image/jpeg";
424 foreach ($types as $m=>$e){
425 if ($ext==$e) $type = $m;
429 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
434 function import_profile_photo($photo,$uid,$cid) {
438 $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
443 $hash = $r[0]['resource-id'];
446 $hash = photo_new_resource();
449 $photo_failure = false;
451 $filename = basename($photo);
452 $img_str = fetch_url($photo,true);
454 // guess mimetype from headers or filename
455 $type = guess_image_type($photo,true);
458 $img = new Photo($img_str, $type);
459 if($img->is_valid()) {
461 $img->scaleImageSquare(175);
463 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
466 $photo_failure = true;
468 $img->scaleImage(80);
470 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
473 $photo_failure = true;
475 $img->scaleImage(48);
477 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
480 $photo_failure = true;
482 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
483 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
484 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
487 $photo_failure = true;
490 $photo = $a->get_baseurl() . '/images/person-175.jpg';
491 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
492 $micro = $a->get_baseurl() . '/images/person-48.jpg';
495 return(array($photo,$thumb,$micro));