X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2FPhoto.php;h=5920f80b38e779389d60188152e6972c03de7d2b;hb=34815de99c548b61c94b6e6939edd485eca728d6;hp=f1e2989d51543e3eb946d35542d5a98ba4a213ef;hpb=5459b00499c01bcf450669bcf42798f0cb049bef;p=friendica.git diff --git a/include/Photo.php b/include/Photo.php index f1e2989d51..5920f80b38 100644 --- a/include/Photo.php +++ b/include/Photo.php @@ -4,6 +4,8 @@ * @brief This file contains the Photo class for image processing */ +use Friendica\App; + require_once("include/photos.php"); class Photo { @@ -47,18 +49,18 @@ class Photo { public function __construct($data, $type=null) { $this->imagick = class_exists('Imagick'); $this->types = $this->supportedTypes(); - if (!array_key_exists($type,$this->types)){ + if (!array_key_exists($type, $this->types)){ $type='image/jpeg'; } $this->type = $type; if ($this->is_imagick() && $this->load_data($data)) { - return true; - } else { - // Failed to load with Imagick, fallback - $this->imagick = false; - } - return $this->load_data($data); + return true; + } else { + // Failed to load with Imagick, fallback + $this->imagick = false; + } + return $this->load_data($data); } public function __destruct() { @@ -68,7 +70,9 @@ class Photo { $this->image->destroy(); return; } - imagedestroy($this->image); + if (is_resource($this->image)) { + imagedestroy($this->image); + } } } @@ -78,6 +82,7 @@ class Photo { /** * @brief Maps Mime types to Imagick formats + * @return arr With with image formats (mime type as key) */ public function get_FormatsMap() { $m = array( @@ -90,11 +95,10 @@ class Photo { private function load_data($data) { if ($this->is_imagick()) { - $this->image = new Imagick(); + $this->image = new Imagick(); try { $this->image->readImageBlob($data); - } - catch (Exception $e) { + } catch (Exception $e) { // Imagick couldn't use the data return false; } @@ -114,7 +118,7 @@ class Photo { */ switch($this->getType()){ case "image/png": - $quality = get_config('system','png_quality'); + $quality = get_config('system', 'png_quality'); if ((! $quality) || ($quality > 9)) { $quality = PNG_QUALITY; } @@ -130,7 +134,7 @@ class Photo { $this->image->setCompressionQuality($quality); break; case "image/jpeg": - $quality = get_config('system','jpeg_quality'); + $quality = get_config('system', 'jpeg_quality'); if ((! $quality) || ($quality > 100)) { $quality = JPEG_QUALITY; } @@ -239,21 +243,21 @@ class Photo { if ((($height * 9) / 16) > $width) { $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } elseif ($width > $height) { // else constrain both dimensions $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } else { - $dest_width = intval(( $width * $max ) / $height); + $dest_width = intval(($width * $max) / $height); $dest_height = $max; } } else { - if ( $width > $max ) { + if ($width > $max) { $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } else { - if ( $height > $max ) { + if ($height > $max) { // very tall image (greater than 16:9) // but width is OK - don't do anything @@ -262,7 +266,7 @@ class Photo { $dest_width = $width; $dest_height = $height; } else { - $dest_width = intval(( $width * $max ) / $height); + $dest_width = intval(($width * $max) / $height); $dest_height = $max; } } else { @@ -283,7 +287,7 @@ class Photo { do { // FIXME - implement horizantal bias for scaling as in followin GD functions - // to allow very tall images to be constrained only horizontally. + // to allow very tall images to be constrained only horizontally. $this->image->scaleImage($dest_width, $dest_height); } while ($this->image->nextImage()); @@ -296,7 +300,7 @@ class Photo { } - $dest = imagecreatetruecolor( $dest_width, $dest_height ); + $dest = imagecreatetruecolor($dest_width, $dest_height); imagealphablending($dest, false); imagesavealpha($dest, true); if ($this->type=='image/png') { @@ -324,6 +328,7 @@ class Photo { return; } + // if script dies at this point check memory_limit setting in php.ini $this->image = imagerotate($this->image,$degrees,0); $this->width = imagesx($this->image); $this->height = imagesy($this->image); @@ -388,12 +393,12 @@ class Photo { return false; } - if ( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') ) { + if ((!function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg')) { return; } $exif = @exif_read_data($filename,null,true); - if (! $exif) { + if (!$exif) { return; } @@ -453,25 +458,25 @@ class Photo { $dest_width = $dest_height = 0; - if ((! $width)|| (! $height)) { + if ((!$width)|| (!$height)) { return false; } if ($width < $min && $height < $min) { if ($width > $height) { $dest_width = $min; - $dest_height = intval(( $height * $min ) / $width); + $dest_height = intval(($height * $min) / $width); } else { - $dest_width = intval(( $width * $min ) / $height); + $dest_width = intval(($width * $min) / $height); $dest_height = $min; } } else { - if ( $width < $min ) { + if ($width < $min) { $dest_width = $min; - $dest_height = intval(( $height * $min ) / $width); + $dest_height = intval(($height * $min) / $width); } else { - if ( $height < $min ) { - $dest_width = intval(( $width * $min ) / $height); + if ($height < $min) { + $dest_width = intval(($width * $min) / $height); $dest_height = $min; } else { $dest_width = $width; @@ -481,10 +486,10 @@ class Photo { } if ($this->is_imagick()) { - return $this->scaleImage($dest_width,$dest_height); + return $this->scaleImage($dest_width, $dest_height); } - $dest = imagecreatetruecolor( $dest_width, $dest_height ); + $dest = imagecreatetruecolor($dest_width, $dest_height); imagealphablending($dest, false); imagesavealpha($dest, true); if ($this->type=='image/png') { @@ -514,7 +519,7 @@ class Photo { return; } - $dest = imagecreatetruecolor( $dim, $dim ); + $dest = imagecreatetruecolor($dim, $dim); imagealphablending($dest, false); imagesavealpha($dest, true); if ($this->type=='image/png') { @@ -530,7 +535,7 @@ class Photo { } - public function cropImage($max,$x,$y,$w,$h) { + public function cropImage($max, $x, $y, $w, $h) { if (!$this->is_valid()) { return false; } @@ -549,7 +554,7 @@ class Photo { return $this->scaleImage($max); } - $dest = imagecreatetruecolor( $max, $max ); + $dest = imagecreatetruecolor($max, $max); imagealphablending($dest, false); imagesavealpha($dest, true); if ($this->type=='image/png') { @@ -590,7 +595,7 @@ class Photo { return $string; } - $quality = FALSE; + $quality = false; ob_start(); @@ -599,18 +604,18 @@ class Photo { switch($this->getType()){ case "image/png": - $quality = get_config('system','png_quality'); - if ((! $quality) || ($quality > 9)) { + $quality = get_config('system', 'png_quality'); + if ((!$quality) || ($quality > 9)) { $quality = PNG_QUALITY; } - imagepng($this->image,NULL, $quality); + imagepng($this->image, null, $quality); break; case "image/jpeg": - $quality = get_config('system','jpeg_quality'); - if ((! $quality) || ($quality > 100)) { + $quality = get_config('system', 'jpeg_quality'); + if ((!$quality) || ($quality > 100)) { $quality = JPEG_QUALITY; } - imagejpeg($this->image,NULL,$quality); + imagejpeg($this->image, null, $quality); } $string = ob_get_contents(); ob_end_clean(); @@ -620,7 +625,7 @@ class Photo { - public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') { + public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '') { $r = q("SELECT `guid` FROM `photo` WHERE `resource-id` = '%s' AND `guid` != '' LIMIT 1", dbesc($rid) @@ -657,7 +662,8 @@ class Photo { `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', - `deny_gid` = '%s' + `deny_gid` = '%s', + `desc` = '%s' WHERE `id` = %d", intval($uid), @@ -679,12 +685,13 @@ class Photo { dbesc($allow_gid), dbesc($deny_cid), dbesc($deny_gid), + dbesc($desc), intval($x[0]['id']) ); } else { $r = q("INSERT INTO `photo` - ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `datasize`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` ) - VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )", + (`uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `datasize`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `desc`) + VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s', '%s')", intval($uid), intval($cid), dbesc($guid), @@ -703,15 +710,11 @@ class Photo { dbesc($allow_cid), dbesc($allow_gid), dbesc($deny_cid), - dbesc($deny_gid) + dbesc($deny_gid), + dbesc($desc) ); } - // Update the cached values - if ($album != 'Contact Photos') { - photo_albums($uid, true); - } - return $r; } } @@ -752,8 +755,8 @@ function guess_image_type($filename, $fromcurl=false) { $ext = pathinfo($filename, PATHINFO_EXTENSION); $types = Photo::supportedTypes(); $type = "image/jpeg"; - foreach ($types as $m=>$e){ - if ($ext==$e) { + foreach ($types as $m => $e){ + if ($ext == $e) { $type = $m; } } @@ -774,9 +777,8 @@ function guess_image_type($filename, $fromcurl=false) { * * @return array Returns array of the different avatar sizes */ -function update_contact_avatar($avatar,$uid,$cid, $force = false) { - - $r = q("SELECT `avatar`, `photo`, `thumb`, `micro` FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid)); +function update_contact_avatar($avatar, $uid, $cid, $force = false) { + $r = q("SELECT `avatar`, `photo`, `thumb`, `micro`, `nurl` FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid)); if (!dbm::is_result($r)) { return false; } else { @@ -784,12 +786,21 @@ function update_contact_avatar($avatar,$uid,$cid, $force = false) { } if (($r[0]["avatar"] != $avatar) OR $force) { - $photos = import_profile_photo($avatar,$uid,$cid, true); + $photos = import_profile_photo($avatar, $uid, $cid, true); if ($photos) { q("UPDATE `contact` SET `avatar` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' WHERE `id` = %d", dbesc($avatar), dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc(datetime_convert()), intval($cid)); + + // Update the public contact (contact id = 0) + if ($uid != 0) { + $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1)); + if (dbm::is_result($pcontact)) { + update_contact_avatar($avatar, 0, $pcontact['id'], $force); + } + } + return $photos; } } @@ -797,9 +808,7 @@ function update_contact_avatar($avatar,$uid,$cid, $force = false) { return $data; } -function import_profile_photo($photo,$uid,$cid, $quit_on_error = false) { - - $a = get_app(); +function import_profile_photo($photo, $uid, $cid, $quit_on_error = false) { $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), @@ -814,41 +823,62 @@ function import_profile_photo($photo,$uid,$cid, $quit_on_error = false) { $photo_failure = false; $filename = basename($photo); - $img_str = fetch_url($photo,true); + $img_str = fetch_url($photo, true); if ($quit_on_error AND ($img_str == "")) { return false; } - $type = guess_image_type($photo,true); + $type = guess_image_type($photo, true); $img = new Photo($img_str, $type); if ($img->is_valid()) { $img->scaleImageSquare(175); - $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 ); + $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4); if ($r === false) $photo_failure = true; $img->scaleImage(80); - $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 ); + $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5); if ($r === false) $photo_failure = true; $img->scaleImage(48); - $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 ); + $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6); 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(); + $suffix = '?ts='.time(); + + $photo = App::get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt() . $suffix; + $thumb = App::get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt() . $suffix; + $micro = App::get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt() . $suffix; + + // Remove the cached photo + $a = get_app(); + $basepath = $a->get_basepath(); + + if (is_dir($basepath."/photo")) { + $filename = $basepath.'/photo/'.$hash.'-4.'.$img->getExt(); + if (file_exists($filename)) { + unlink($filename); + } + $filename = $basepath.'/photo/'.$hash.'-5.'.$img->getExt(); + if (file_exists($filename)) { + unlink($filename); + } + $filename = $basepath.'/photo/'.$hash.'-6.'.$img->getExt(); + if (file_exists($filename)) { + unlink($filename); + } + } } else { $photo_failure = true; } @@ -858,9 +888,9 @@ function import_profile_photo($photo,$uid,$cid, $quit_on_error = false) { } if ($photo_failure) { - $photo = $a->get_baseurl() . '/images/person-175.jpg'; - $thumb = $a->get_baseurl() . '/images/person-80.jpg'; - $micro = $a->get_baseurl() . '/images/person-48.jpg'; + $photo = App::get_baseurl() . '/images/person-175.jpg'; + $thumb = App::get_baseurl() . '/images/person-80.jpg'; + $micro = App::get_baseurl() . '/images/person-48.jpg'; } return(array($photo,$thumb,$micro)); @@ -872,7 +902,7 @@ function get_photo_info($url) { $data = Cache::get($url); - if (is_null($data) OR !$data) { + if (is_null($data) OR !$data OR !is_array($data)) { $img_str = fetch_url($url, true, $redirects, 4); $filesize = strlen($img_str); @@ -915,21 +945,21 @@ function scale_image($width, $height, $max) { if ((($height * 9) / 16) > $width) { $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } elseif ($width > $height) { // else constrain both dimensions $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } else { - $dest_width = intval(( $width * $max ) / $height); + $dest_width = intval(($width * $max) / $height); $dest_height = $max; } } else { - if ( $width > $max ) { + if ($width > $max) { $dest_width = $max; - $dest_height = intval(( $height * $max ) / $width); + $dest_height = intval(($height * $max) / $width); } else { - if ( $height > $max ) { + if ($height > $max) { // very tall image (greater than 16:9) // but width is OK - don't do anything @@ -938,7 +968,7 @@ function scale_image($width, $height, $max) { $dest_width = $width; $dest_height = $height; } else { - $dest_width = intval(( $width * $max ) / $height); + $dest_width = intval(($width * $max) / $height); $dest_height = $max; } } else { @@ -950,7 +980,7 @@ function scale_image($width, $height, $max) { return array("width" => $dest_width, "height" => $dest_height); } -function store_photo($a, $uid, $imagedata = "", $url = "") { +function store_photo(App $a, $uid, $imagedata = "", $url = "") { $r = q("SELECT `user`.`nickname`, `user`.`page-flags`, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `user`.`blocked` = 0 AND `contact`.`self` = 1 LIMIT 1", intval($uid)); @@ -977,7 +1007,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { $a->save_timestamp($stamp1, "file"); } - $maximagesize = get_config('system','maximagesize'); + $maximagesize = get_config('system', 'maximagesize'); if (($maximagesize) && (strlen($imagedata) > $maximagesize)) { logger("Image exceeds size limit of ".$maximagesize, LOGGER_DEBUG); @@ -1022,7 +1052,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { $ph->orient($tempfile); unlink($tempfile); - $max_length = get_config('system','max_image_length'); + $max_length = get_config('system', 'max_image_length'); if (! $max_length) { $max_length = MAX_IMAGE_LENGTH; } @@ -1040,7 +1070,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { // Pictures are always public by now //$defperm = '<'.$default_cid.'>'; $defperm = ""; - $visitor = 0; + $visitor = 0; $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 0, 0, $defperm); @@ -1049,18 +1079,18 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { return(array()); } - $image = array("page" => $a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash, - "full" => $a->get_baseurl()."/photo/{$hash}-0.".$ph->getExt()); + $image = array("page" => App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash, + "full" => App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt()); if ($width > 800 || $height > 800) { - $image["large"] = $a->get_baseurl()."/photo/{$hash}-0.".$ph->getExt(); + $image["large"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt(); } if ($width > 640 || $height > 640) { $ph->scaleImage(640); $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 1, 0, $defperm); if ($r) { - $image["medium"] = $a->get_baseurl()."/photo/{$hash}-1.".$ph->getExt(); + $image["medium"] = App::get_baseurl()."/photo/{$hash}-1.".$ph->getExt(); } } @@ -1068,7 +1098,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { $ph->scaleImage(320); $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 2, 0, $defperm); if ($r) { - $image["small"] = $a->get_baseurl()."/photo/{$hash}-2.".$ph->getExt(); + $image["small"] = App::get_baseurl()."/photo/{$hash}-2.".$ph->getExt(); } } @@ -1093,7 +1123,7 @@ function store_photo($a, $uid, $imagedata = "", $url = "") { $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 3, 0, $defperm); if ($r) { - $image["thumb"] = $a->get_baseurl()."/photo/{$hash}-3.".$ph->getExt(); + $image["thumb"] = App::get_baseurl()."/photo/{$hash}-3.".$ph->getExt(); } }