iOS使用快捷指令上传压缩后的截屏到Google Photos

注:本方法需要一台位于境外可以访问Google的服务器,或在家中配置科学上网后搭建服务器使用

最近Google Photo要收费了,想想我这5年来相册里备份的不是截屏就是涩图,目前为止已经占用了47G了(rclone拉到本地做备份后统计的大小,已经过Google压缩)。虽然不贵但是以这个增长速度来看总感觉很亏。
听闻HEIF压缩率惊人,在手机上测试发现果真如此。Google相册也支持上传,但可能由于版权原因并不给用户提供转换功能。所以需要在手机上完成转换后再上传。

步骤如下:
1、iOS端通过Shortcut获取相册内所有照片,逐一转换为HEIF格式,判断前后大小变化并上传至服务器,最后删除本地照片。
2、服务器端通过exiftool将照片日期写入EXIF中(因为rclone不能同步图片文件本身的修改时间到Google 传送门
3、服务器端将写入完的图片移入rclone的挂载目录中(具体为GooglePhoto:upload/目录下)
4、关闭Google Photo自动上传

首先是Shortcut脚本

链接:https://www.icloud.com/shortcuts/278f6f64ddf4409d9453636be5c49382

服务端下载Exiftool并解压

cd /opt
wget https://exiftool.org/Image-ExifTool-12.23.tar.gz
tar -zxf Image-ExifTool-12.23.tar.gz

安装Nginx + PHP(具体步骤网上烂大街了就不写了)

安装rclone,添加Photos配置(这里以debian为例,centos请自行去官网下载rpm包)

cd ~/
wget https://downloads.rclone.org/v1.55.0/rclone-v1.55.0-linux-amd64.deb
dpkg -i rclone-v1.55.0-linux-amd64.deb
rclone config
# n) New remote
# name> GooglePhoto
## 16 / Google Photos
##   \ "google photos"
# Storage> 16
## OAuth Client Id
## Leave blank normally.
## Enter a string value. Press Enter for the default ("").
# client_id>
## OAuth Client Secret
## Leave blank normally.
## Enter a string value. Press Enter for the default ("").
# client_secret>
## Set to make the Google Photos backend read only.
## 
## If you choose read only then rclone will only request read only access
## to your photos, otherwise rclone will request full access.
## Enter a boolean value (true or false). Press Enter for the default ("false").
# read_only>
## Edit advanced config? (y/n)
## y) Yes
## n) No (default)
# y/n> n
## Remote config
## Use auto config?
##  * Say Y if not sure
##  * Say N if you are working on a remote or headless machine
## y) Yes (default)
## n) No
# y/n> n
## Please go to the following link: https://accounts.google.com/o/oauth2/auth?XXXXXXXXXXXXXXXXXX
## Log in and authorize rclone for access
# Enter verification code> XXXXXXXXXX
## --------------------
## [XXXXX]
## client_id = 
## client_secret = 
## token = XXXXXX
## --------------------
## y) Yes this is OK
## e) Edit this remote
## d) Delete this remote
# y/e/d> y

挂载Photos到本地目录(只写)

apt update && apt install -y fuse
mkdir /mnt/GooglePhotos

cat > /etc/systemd/system/rclonephotos.service <<EOF
[Unit]
Description=rclone GooglePhotos

[Service]
User=root
ExecStart=/usr/bin/rclone mount GooglePhoto:upload/ /mnt/GooglePhotos/ --allow-other --allow-non-empty --vfs-cache-mode full --default-permissions --uid 【www-data的uid】 --gid 【www-data的gid】 --umask 027
ExecStop=/usr/bin/fusermount -u /mnt/GooglePhotos/
Restart=on-abort

[Install]
WantedBy=multi-user.target
EOF

systemctl enable rclonephotos --now

service rclonephotos status
## Active: active (running)

ls -l /mnt
## drwxr-x--- 1 www-data www-data    0 Apr 12 16:17 GooglePhotos

最后是上传脚本(文件验证懒得做了反正有密码)

<?php
ini_set('display_errors',1);
date_default_timezone_set('Asia/Shanghai');
header('Content-Type:text/plain; charset=utf-8');
if((!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) || !($_SERVER['PHP_AUTH_USER'] == 'test' && $_SERVER['PHP_AUTH_PW'] == 'testtest')){
    header('WWW-Authenticate: Basic realm="Authentication"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authentication invalid.';
    exit;
}
if(empty($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])){
    header('HTTP/1.0 403 Forbidden');
    echo 'Invalid file source.';
    exit;
}
@unlink("/tmp/{$_FILES['file']['name']}");
if(move_uploaded_file($_FILES['file']['tmp_name'],"/tmp/{$_FILES['file']['name']}") === FALSE){
    header('HTTP/1.0 500 Internal Server Error');
    echo 'Could not move temporary file.';
    exit;
}
$time = strtotime($_POST['time']);
$exiftime = date('Y:m:d H:i:s',$time);
exec("/opt/Image-ExifTool-12.23/exiftool -alldates='$exiftime' /tmp/{$_FILES['file']['name']}");
rename("/tmp/{$_FILES['file']['name']}","/mnt/GooglePhotos/{$_FILES['file']['name']}");
echo 'Success.';

最终效果:

原文件GooglePhoto高画质HEIF格式
(Google这个根本没在压啊(ノ`□´)ノ⌒┻━┻)

标签: ios, shortcut, photos, google photos, heif, 快捷指令, 图片压缩, heic

仅有一条评论

  1. 噗哈哈,这google压了个寂寞

添加新评论