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 $exif = exif_read_data($filename);
147 $ort = $exif['Orientation'];
154 case 2: // horizontal flip
158 case 3: // 180 rotate left
162 case 4: // vertical flip
163 $this->flip(false, true);
166 case 5: // vertical flip + 90 rotate right
167 $this->flip(false, true);
171 case 6: // 90 rotate right
175 case 7: // horizontal flip + 90 rotate right
180 case 8: // 90 rotate left
188 public function scaleImageUp($min) {
190 $width = $this->width;
191 $height = $this->height;
193 $dest_width = $dest_height = 0;
195 if((! $width)|| (! $height))
198 if($width < $min && $height < $min) {
199 if($width > $height) {
201 $dest_height = intval(( $height * $min ) / $width);
204 $dest_width = intval(( $width * $min ) / $height);
209 if( $width < $min ) {
211 $dest_height = intval(( $height * $min ) / $width);
214 if( $height < $min ) {
215 $dest_width = intval(( $width * $min ) / $height);
219 $dest_width = $width;
220 $dest_height = $height;
226 $dest = imagecreatetruecolor( $dest_width, $dest_height );
227 imagealphablending($dest, false);
228 imagesavealpha($dest, true);
229 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
230 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
232 imagedestroy($this->image);
233 $this->image = $dest;
234 $this->width = imagesx($this->image);
235 $this->height = imagesy($this->image);
241 public function scaleImageSquare($dim) {
243 $dest = imagecreatetruecolor( $dim, $dim );
244 imagealphablending($dest, false);
245 imagesavealpha($dest, true);
246 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
247 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
249 imagedestroy($this->image);
250 $this->image = $dest;
251 $this->width = imagesx($this->image);
252 $this->height = imagesy($this->image);
256 public function cropImage($max,$x,$y,$w,$h) {
257 $dest = imagecreatetruecolor( $max, $max );
258 imagealphablending($dest, false);
259 imagesavealpha($dest, true);
260 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
261 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
263 imagedestroy($this->image);
264 $this->image = $dest;
265 $this->width = imagesx($this->image);
266 $this->height = imagesy($this->image);
269 public function saveImage($path) {
272 $quality = get_config('system','png_quality');
273 if((! $quality) || ($quality > 9))
274 $quality = PNG_QUALITY;
275 imagepng($this->image, $path, $quality);
278 $quality = get_config('system','jpeg_quality');
279 if((! $quality) || ($quality > 100))
280 $quality = JPEG_QUALITY;
281 imagejpeg($this->image,$path,$quality);
286 public function imageString() {
290 $quality = get_config('system','png_quality');
291 if((! $quality) || ($quality > 9))
292 $quality = PNG_QUALITY;
293 imagepng($this->image,NULL, $quality);
296 $quality = get_config('system','jpeg_quality');
297 if((! $quality) || ($quality > 100))
298 $quality = JPEG_QUALITY;
300 imagejpeg($this->image,NULL,$quality);
302 $s = ob_get_contents();
309 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
311 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
315 $guid = $r[0]['guid'];
319 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
326 $r = q("UPDATE `photo`
330 `resource-id` = '%s',
345 where id = %d limit 1",
351 dbesc(datetime_convert()),
352 dbesc(datetime_convert()),
353 dbesc(basename($filename)),
356 intval($this->height),
357 intval($this->width),
358 dbesc($this->imageString()),
369 $r = q("INSERT INTO `photo`
370 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
371 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
376 dbesc(datetime_convert()),
377 dbesc(datetime_convert()),
378 dbesc(basename($filename)),
381 intval($this->height),
382 intval($this->width),
383 dbesc($this->imageString()),
398 * Guess image mimetype from filename or from Content-Type header
400 * @arg $filename string Image filename
401 * @arg $fromcurl boolean Check Content-Type header from curl request
403 function guess_image_type($filename, $fromcurl=false) {
404 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
409 $h = explode("\n",$a->get_curl_headers());
411 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
414 if (array_key_exists('Content-Type', $headers))
415 $type = $headers['Content-Type'];
418 $ext = pathinfo($filename, PATHINFO_EXTENSION);
419 $types = Photo::supportedTypes();
420 $type = "image/jpeg";
421 foreach ($types as $m=>$e){
422 if ($ext==$e) $type = $m;
426 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
431 function import_profile_photo($photo,$uid,$cid) {
435 $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
440 $hash = $r[0]['resource-id'];
443 $hash = photo_new_resource();
446 $photo_failure = false;
448 $filename = basename($photo);
449 $img_str = fetch_url($photo,true);
451 // guess mimetype from headers or filename
452 $type = guess_image_type($photo,true);
455 $img = new Photo($img_str, $type);
456 if($img->is_valid()) {
458 $img->scaleImageSquare(175);
460 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
463 $photo_failure = true;
465 $img->scaleImage(80);
467 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
470 $photo_failure = true;
472 $img->scaleImage(48);
474 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
477 $photo_failure = true;
479 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
480 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
481 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
484 $photo_failure = true;
487 $photo = $a->get_baseurl() . '/images/person-175.jpg';
488 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
489 $micro = $a->get_baseurl() . '/images/person-48.jpg';
492 return(array($photo,$thumb,$micro));