]> git.mxchange.org Git - friendica.git/blobdiff - include/Photo.php
I want HTML code!
[friendica.git] / include / Photo.php
index ffefc37eca34e0cc959662b04b971fad827c4104..3af1691ee7250880479de51ec3573fb5e2839a05 100644 (file)
@@ -105,9 +105,9 @@ class Photo {
 
 
                $dest = imagecreatetruecolor( $dest_width, $dest_height );
-               imagealphablending($dest->image, false);
+               imagealphablending($dest, false);
                imagesavealpha($dest, true);
-               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
+               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
                imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
                if($this->image)
                        imagedestroy($this->image);
@@ -121,7 +121,70 @@ class Photo {
                $this->image  = imagerotate($this->image,$degrees,0);
                $this->width  = imagesx($this->image);
                $this->height = imagesy($this->image);
-       }       
+       }
+
+       public function flip($horiz = true, $vert = false) {
+               $w = imagesx($this->image);
+               $h = imagesy($this->image);
+               $flipped = imagecreate($w, $h);
+               if($horiz) {
+                       for ($x = 0; $x < $w; $x++) {
+                               imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
+                       }
+               }
+               if($vert) {
+                       for ($y = 0; $y < $h; $y++) {
+                               imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
+                       }
+               }
+               $this->image = $flipped;
+       }
+
+       public function orient($filename) {
+               // based off comment on http://php.net/manual/en/function.imagerotate.php
+
+               if(! function_exists('exif_read_data'))
+                       return;
+
+               $exif = exif_read_data($filename);
+               $ort = $exif['Orientation'];
+
+               switch($ort)
+               {
+                       case 1: // nothing
+                       break;
+
+               case 2: // horizontal flip
+                   $this->flip();
+                   break;
+                               
+               case 3: // 180 rotate left
+               $this->rotate(180);
+                       break;
+                   
+               case 4: // vertical flip
+               $this->flip(false, true);
+                       break;
+               
+               case 5: // vertical flip + 90 rotate right
+               $this->flip(false, true);
+                $this->rotate(-90);
+                       break;
+               
+               case 6: // 90 rotate right
+                   $this->rotate(-90);
+                       break;
+               
+               case 7: // horizontal flip + 90 rotate right
+                   $this->flip();   
+                   $this->rotate(-90);
+                       break;
+               
+               case 8:    // 90 rotate left
+                   $this->rotate(90);
+                       break;
+           }
+       }
 
 
 
@@ -164,9 +227,9 @@ class Photo {
 
 
                $dest = imagecreatetruecolor( $dest_width, $dest_height );
-               imagealphablending($dest->image, false);
+               imagealphablending($dest, false);
                imagesavealpha($dest, true);
-               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
+               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
                imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
                if($this->image)
                        imagedestroy($this->image);
@@ -181,9 +244,9 @@ class Photo {
        public function scaleImageSquare($dim) {
 
                $dest = imagecreatetruecolor( $dim, $dim );
-               imagealphablending($dest->image, false);
+               imagealphablending($dest, false);
                imagesavealpha($dest, true);
-               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
+               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
                imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
                if($this->image)
                        imagedestroy($this->image);
@@ -195,9 +258,9 @@ class Photo {
 
        public function cropImage($max,$x,$y,$w,$h) {
                $dest = imagecreatetruecolor( $max, $max );
-               imagealphablending($dest->image, false);
+               imagealphablending($dest, false);
                imagesavealpha($dest, true);
-               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127));; // fill with alpha
+               if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
                imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
                if($this->image)
                        imagedestroy($this->image);
@@ -256,35 +319,81 @@ class Photo {
                else
                        $guid = get_guid();
 
-               $r = q("INSERT INTO `photo`
-                       ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
-                       VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
-                       intval($uid),
-                       intval($cid),
-                       dbesc($guid),
-                       dbesc($rid),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       dbesc(basename($filename)),
-                       dbesc($this->type),
-                       dbesc($album),
-                       intval($this->height),
-                       intval($this->width),
-                       dbesc($this->imageString()),
-                       intval($scale),
-                       intval($profile),
-                       dbesc($allow_cid),
-                       dbesc($allow_gid),
-                       dbesc($deny_cid),
-                       dbesc($deny_gid)
+               $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
+                               dbesc($rid),
+                               intval($uid),
+                               intval($cid),
+                               intval($scale)
                );
+               if(count($x)) {
+                       $r = q("UPDATE `photo`
+                               set `uid` = %d, 
+                               `contact-id` = %d, 
+                               `guid` = '%s', 
+                               `resource-id` = '%s', 
+                               `created` = '%s',
+                               `edited` = '%s', 
+                               `filename` = '%s', 
+                               `type` = '%s', 
+                               `album` = '%s', 
+                               `height` = %d, 
+                               `width` = %d, 
+                               `data` = '%s', 
+                               `scale` = %d, 
+                               `profile` = %d, 
+                               `allow_cid` = '%s', 
+                               `allow_gid` = '%s', 
+                               `deny_cid` = '%s',
+                               `deny_gid` = '%s' 
+                               where id = %d limit 1",
+
+                               intval($uid),
+                               intval($cid),
+                               dbesc($guid),
+                               dbesc($rid),
+                               dbesc(datetime_convert()),
+                               dbesc(datetime_convert()),
+                               dbesc(basename($filename)),
+                               dbesc($this->type),
+                               dbesc($album),
+                               intval($this->height),
+                               intval($this->width),
+                               dbesc($this->imageString()),
+                               intval($scale),
+                               intval($profile),
+                               dbesc($allow_cid),
+                               dbesc($allow_gid),
+                               dbesc($deny_cid),
+                               dbesc($deny_gid),
+                               intval($x[0]['id'])
+                       );
+               }
+               else {
+                       $r = q("INSERT INTO `photo`
+                               ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
+                               VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
+                               intval($uid),
+                               intval($cid),
+                               dbesc($guid),
+                               dbesc($rid),
+                               dbesc(datetime_convert()),
+                               dbesc(datetime_convert()),
+                               dbesc(basename($filename)),
+                               dbesc($this->type),
+                               dbesc($album),
+                               intval($this->height),
+                               intval($this->width),
+                               dbesc($this->imageString()),
+                               intval($scale),
+                               intval($profile),
+                               dbesc($allow_cid),
+                               dbesc($allow_gid),
+                               dbesc($deny_cid),
+                               dbesc($deny_gid)
+                       );
+               }
                return $r;
        }
-
-
-
-
-
 }}
 
 
@@ -295,6 +404,7 @@ class Photo {
  * @arg $fromcurl boolean Check Content-Type header from curl request
  */
 function guess_image_type($filename, $fromcurl=false) {
+    logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
        $type = null;
        if ($fromcurl) {
                $a = get_app(); 
@@ -310,12 +420,13 @@ function guess_image_type($filename, $fromcurl=false) {
        if (is_null($type)){
                $ext = pathinfo($filename, PATHINFO_EXTENSION);
                $types = Photo::supportedTypes();
+               $type = "image/jpeg";
                foreach ($types as $m=>$e){
-                       if ($ext==$e) return $m;
+                       if ($ext==$e) $type = $m;
                }
-               $type = "image/jpeg";
-       }
 
+       }
+    logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
        return $type;
        
 }
@@ -324,6 +435,17 @@ function import_profile_photo($photo,$uid,$cid) {
 
        $a = get_app();
 
+       $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
+               intval($uid),
+               intval($cid)
+       );
+       if(count($r)) {
+               $hash = $r[0]['resource-id'];
+       }
+       else {
+               $hash = photo_new_resource();
+       }
+               
        $photo_failure = false;
 
        $filename = basename($photo);
@@ -338,8 +460,6 @@ function import_profile_photo($photo,$uid,$cid) {
 
                $img->scaleImageSquare(175);
                                        
-               $hash = photo_new_resource();
-
                $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
 
                if($r === false)
@@ -359,8 +479,6 @@ function import_profile_photo($photo,$uid,$cid) {
                if($r === false)
                        $photo_failure = true;
 
-
-
                $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
                $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
                $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();