big changes to photo->store() which is now photo->save() and takes an array instead of a list of args. Also the beginning of the migration to using photo_flags to indicate special purpose photos such as profile photos and contact photos and "thing" photos.

This commit is contained in:
friendica 2013-08-07 01:42:45 -07:00
parent 809a53a2f8
commit 20b22421d3
4 changed files with 172 additions and 20 deletions

View file

@ -271,6 +271,111 @@ abstract class photo_driver {
}
public function save($arr) {
$p = array();
$p['aid'] = ((intval($arr['aid'])) ? intval($arr['aid']) : 0);
$p['uid'] = ((intval($arr['uid'])) ? intval($arr['uid']) : 0);
$p['xchan'] = (($arr['xchan']) ? $arr['xchan'] : '');
$p['resource_id'] = (($arr['resource_id']) ? $arr['resource_id'] : '');
$p['filename'] = (($arr['filename']) ? $arr['filename'] : '');
$p['album'] = (($arr['album']) ? $arr['album'] : '');
$p['scale'] = ((intval($arr['scale'])) ? intval($arr['scale']) : 0);
$p['photo_flags'] = ((intval($arr['photo_flags'])) ? intval($arr['photo_flags']) : 0);
$p['allow_cid'] = (($arr['allow_cid']) ? $arr['allow_cid'] : '');
$p['allow_gid'] = (($arr['allow_gid']) ? $arr['allow_gid'] : '');
$p['deny_cid'] = (($arr['deny_cid']) ? $arr['deny_cid'] : '');
$p['deny_gid'] = (($arr['deny_gid']) ? $arr['deny_gid'] : '');
// temporary until we get rid of photo['profile'] and just use photo['photo_flags']
// but this will require updating all existing photos in the DB.
$p['profile'] = (($p['photo_flags'] & PHOTO_PROFILE) ? 1 : 0);
$x = q("select id from photo where resource_id = '%s' and uid = %d and xchan = '%s' and `scale` = %d limit 1",
dbesc($p['resource_id']),
intval($p['uid']),
dbesc($p['xchan']),
intval($p['scale'])
);
if($x) {
$r = q("UPDATE `photo` set
`aid` = %d,
`uid` = %d,
`xchan` = '%s',
`resource_id` = '%s',
`created` = '%s',
`edited` = '%s',
`filename` = '%s',
`type` = '%s',
`album` = '%s',
`height` = %d,
`width` = %d,
`data` = '%s',
`size` = %d,
`scale` = %d,
`profile` = %d,
`photo_flags` = %d,
`allow_cid` = '%s',
`allow_gid` = '%s',
`deny_cid` = '%s',
`deny_gid` = '%s'
where id = %d limit 1",
intval($p['aid']),
intval($p['uid']),
dbesc($p['xchan']),
dbesc($p['resource_id']),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc(basename($p['filename'])),
dbesc($this->getType()),
dbesc($p['album']),
intval($this->getHeight()),
intval($this->getWidth()),
dbesc($this->imageString()),
intval(strlen($this->imageString())),
intval($p['scale']),
intval($p['profile']),
intval($p['photo_flags']),
dbesc($p['allow_cid']),
dbesc($p['allow_gid']),
dbesc($p['deny_cid']),
dbesc($p['deny_gid']),
intval($x[0]['id'])
);
}
else {
$r = q("INSERT INTO `photo`
( `aid`, `uid`, `xchan`, `resource_id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `size`, `scale`, `profile`, `photo_flags`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s' )",
intval($p['aid']),
intval($p['uid']),
dbesc($p['xchan']),
dbesc($p['resource_id']),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc(basename($filename)),
dbesc($this->getType()),
dbesc($p['album']),
intval($this->getHeight()),
intval($this->getWidth()),
dbesc($this->imageString()),
intval(strlen($this->imageString())),
intval($p['scale']),
intval($p['profile']),
intval($p['photo_flags']),
dbesc($p['allow_cid']),
dbesc($p['allow_gid']),
dbesc($p['deny_cid']),
dbesc($p['deny_gid'])
);
}
return $r;
}
public function store($aid, $uid, $xchan, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
$x = q("select id from photo where `resource_id` = '%s' and uid = %d and `xchan` = '%s' and `scale` = %d limit 1",
@ -356,6 +461,11 @@ abstract class photo_driver {
/**
* Guess image mimetype from filename or from Content-Type header
*
@ -434,21 +544,25 @@ function import_profile_photo($photo,$xchan) {
$img->scaleImageSquare(175);
$r = $img->store(0, 0, $xchan, $hash, $filename, 'Contact Photos', 4 );
$p = array('xchan' => $xchan,'resource_id' => $hash, 'filename' => 'Contact Photos', 'photo_flags' => PHOTO_XCHAN, 'scale' => 4);
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(80);
$p['scale'] = 5;
$r = $img->store(0, 0, $xchan, $hash, $filename, 'Contact Photos', 5 );
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(48);
$p['scale'] = 6;
$r = $img->store(0, 0, $xchan, $hash, $filename, 'Contact Photos', 6 );
$r = $img->save($p);
if($r === false)
$photo_failure = true;
@ -492,21 +606,25 @@ function import_channel_photo($photo,$type,$aid,$uid) {
$img->scaleImageSquare(175);
$r = $img->store($aid,$uid,'', $hash, $filename, t('Profile Photos'), 4, true);
$p = array('aid' => $aid, 'uid' => $uid, 'resource_id' => $hash, 'filename' => $filename, 'album' => t('Profile Photos'), 'photo_flags' => PHOTO_PROFILE, 'scale' => 4);
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(80);
$p['scale'] = 5;
$r = $img->store($aid,$uid,'', $hash, $filename, t('Profile Photos'), 5, true);
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(48);
$p['scale'] = 6;
$r = $img->store($aid,$uid,'', $hash, $filename, t('Profile Photos'), 6, true);
$r = $img->save($p);
if($r === false)
$photo_failure = true;

View file

@ -150,13 +150,20 @@ function photo_upload($channel, $observer, $args) {
$errors = false;
$r1 = $ph->store($account_id, $channel_id, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$p = array('aid' => $account_id, 'uid' => $channel_id, 'xchan' => $visitor, 'resource_id' => $photo_hash,
'filename' => $filename, 'album' => $album, 'scale' => 0, 'photo_flags' => PHOTO_NORMAL,
'allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny
);
$r1 = $ph->save($p);
if(! $r1)
$errors = true;
if(($width > 640 || $height > 640) && (! $errors)) {
$ph->scaleImage(640);
$r2 = $ph->store($account_id, $channel_id, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$p['scale'] = 1;
$r2 = $ph->save($p);
$smallest = 1;
if(! $r2)
$errors = true;
@ -164,7 +171,8 @@ function photo_upload($channel, $observer, $args) {
if(($width > 320 || $height > 320) && (! $errors)) {
$ph->scaleImage(320);
$r3 = $ph->store($account_id, $channel_id, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
$p['scale'] = 2;
$r3 = $ph->save($p);
$smallest = 2;
if(! $r3)
$errors = true;
@ -302,7 +310,7 @@ function photos_list_photos($channel,$observer,$album = '') {
$ret = array('success' => false);
$r = q("select resource_id, created, edited, title, `desc`, album, filename, `type`, height, width, `size`, `scale`, profile, allow_cid, allow_gid, deny_cid, deny_gid from photo where uid = %d and ( photo_flags = %d or photo_flags = %d ) $sql_extra ",
$r = q("select resource_id, created, edited, title, `desc`, album, filename, `type`, height, width, `size`, `scale`, profile, photo_flags, allow_cid, allow_gid, deny_cid, deny_gid from photo where uid = %d and ( photo_flags = %d or photo_flags = %d ) $sql_extra ",
intval($channel_id),
intval(PHOTO_NORMAL),
intval(PHOTO_PROFILE)

View file

@ -84,18 +84,23 @@ function profile_photo_post(&$a) {
$aid = get_account_id();
$r1 = $im->store($aid, local_user(), '', $base_image['resource_id'],$base_image['filename'],
t('Profile Photos'), 4, $is_default_profile);
$p = array('aid' => $aid, 'uid' => local_user(), 'resource_id' => $base_image['resource_id'],
'filename' => $base_image['filename'], 'album' => t('Profile Photos'));
$p['scale'] = 4;
$p['photo_flags'] = (($is_default_profile) ? PHOTO_PROFILE : PHOTO_NORMAL);
$r1 = $im->save($p);
$im->scaleImage(80);
$p['scale'] = 5;
$r2 = $im->store($aid, local_user(), '', $base_image['resource_id'],$base_image['filename'],
t('Profile Photos'), 5, $is_default_profile);
$r2 = $im->save($p);
$im->scaleImage(48);
$p['scale'] = 6;
$r3 = $im->store($aid, local_user(), '', $base_image['resource_id'],$base_image['filename'],
t('Profile Photos'), 6, $is_default_profile);
$r3 = $im->save($p);
if($r1 === false || $r2 === false || $r3 === false) {
// if one failed, delete them all so we can start over.
@ -114,6 +119,13 @@ function profile_photo_post(&$a) {
dbesc($base_image['resource_id']),
intval(local_user())
);
$r = q("UPDATE photo SET ( photo_flags ^ %d ) WHERE (photo_flags & %d )
AND resource_id != '%s' AND `uid` = %d",
intval(PHOTO_PROFILE),
intval(PHOTO_PROFILE),
dbesc($base_image['resource_id']),
intval(local_user())
);
}
else {
$r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d limit 1",
@ -224,6 +236,10 @@ function profile_photo_content(&$a) {
// unset any existing profile photos
$r = q("UPDATE photo SET profile = 0 WHERE profile = 1 AND uid = %d",
intval(local_user()));
$r = q("UPDATE photo SET (photo_flags ^ %d ) WHERE (photo_flags & %d ) AND uid = %d",
intval(PHOTO_PROFILE),
intval(PHOTO_PROFILE),
intval(local_user()));
// set all sizes of this one as profile photos
$r = q("UPDATE photo SET profile = 1 WHERE uid = %d AND resource_id = '%s'",
@ -231,6 +247,12 @@ function profile_photo_content(&$a) {
dbesc($resource_id)
);
$r = q("UPDATE photo SET photo_flags = ( photo_flags | %d ) WHERE uid = %d AND resource_id = '%s'",
intval(PHOTO_PROFILE),
intval(local_user()),
dbesc($resource_id)
);
$r = q("UPDATE xchan set xchan_photo_date = '%s'
where xchan_hash = '%s' limit 1",
dbesc(datetime_convert()),
@ -241,7 +263,7 @@ function profile_photo_content(&$a) {
goaway($a->get_baseurl() . '/profiles');
}
$r = q("SELECT data, type FROM photo WHERE id = %d and uid = %d limit 1",
$r = q("SELECT `data`, `type` FROM photo WHERE id = %d and uid = %d limit 1",
intval($r[0]['id']),
intval(local_user())
@ -320,7 +342,9 @@ function profile_photo_crop_ui_head(&$a, $ph){
$hash = photo_new_resource();
$smallest = 0;
$r = $ph->store(get_account_id(), local_user(), '', $hash, $filename, t('Profile Photos'), 0 );
$p = array('aid' => get_account_id(), 'uid' => local_user(), 'resource_id' => $hash,
'filename' => $filename, 'album' => t('Profile Photos'), 'scale' => 0);
$r = $ph->save($p);
if($r)
info( t('Image uploaded successfully.') . EOL );
@ -329,7 +353,9 @@ function profile_photo_crop_ui_head(&$a, $ph){
if($width > 640 || $height > 640) {
$ph->scaleImage(640);
$r = $ph->store(get_account_id(), local_user(), '' , $hash, $filename, t('Profile Photos'), 1 );
$p['scale'] = 1;
$r = $ph->save($p);
if($r === false)
notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );

View file

@ -1 +1 @@
2013-08-06.397
2013-08-07.398