普通视图

如何使用 PHP 脚本筛选去不图床已到期用户邮箱

2025年10月16日 00:00

因为图床程序暂不支持套餐到期通知,需要杜老师定期统计到期的用户,再通过邮箱发通知。每次统计时操作较麻烦,为了方便统计用户,尝试用 PHP 编写一个页面,可以筛选去不图床到期用户邮箱。

公告

去不图床会在用户的容量套餐到期后,发送邮件通知「请确保注册邮箱可用性」

通知内容包含数据到期时间「一般为容量到期后七天」如 5 月 24 日到期的用户,我们会保留数据到 5 月 31 日。

超过保留期限的数据会从数据库清除,但数据依然会保留在备份节点中。

为了方便用户从备份节点中恢复数据,去不图床定期「每半小时」同步至 OneDrive,需要的小伙伴可以访问 共享链接 获取备份数据,页面密码为 7bu.top

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// 数据库配置
$host = 'localhost'; // 数据库主机
$username = 'your_username'; // 数据库用户名
$password = 'your_password'; // 数据库密码
$database = 'your_database'; // 数据库名称

// 创建数据库连接
$conn = new mysqli($host, $username, $password, $database);

// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}

// 执行查询
$sql = "SELECT * FROM `users` WHERE `capacity` = 0.00 AND `size` != 0.0000";
$result = $conn->query($sql);

// 检查是否有结果返回
if ($result->num_rows > 0) {
// 打开文件准备写入
$file = fopen("1.txt", "w");

// 写入表头
$headers = [];
while ($fieldinfo = $result->fetch_field()) {
$headers[] = $fieldinfo->name;
}
fwrite($file, implode("\t", $headers) . "\n");

// 写入每一行数据
while ($row = $result->fetch_assoc()) {
fwrite($file, implode("\t", $row) . "\n");
}

// 关闭文件句柄
fclose($file);

echo "数据已成功写入 1.txt";
} else {
echo "没有匹配的记录";
}

// 关闭数据库连接
$conn->close();
?>

注意:以上是一个完整 PHP 脚本,用于连接 MySQL 数据库、执行 SQL 查询并将结果写入到 1.txt 文件中。

使用说明

  1. 将上述的代码保存为一个.php 文件如 export_users.php

  2. 替换脚本中的数据库信息 your_username/your_password/your_database 为您自己的实际数据库凭证;

  3. 确保运行该脚本的服务器支持 PHP 并且可以访问 MySQL 数据库;

  4. 当通过浏览器或命令行访问这个 PHP 页面时,它会执行查询并将结果写入到当前目录下的 1.txt 文件。

输出示例

1
2
3
4
email
123123123@qq.com
456456456@qq.com
789789789@qq.com

注意:确保 PHP 环境有权限写入目标文件「如 1.txt

Ubuntu 系统无法通过 pip 命令安装 Python 库问题解决

2025年9月22日 00:00

为了更好的执行 Python 脚本,杜老师习惯使用 Ubuntu 系统,不过在安装 Python 库时经常报错。收集了安装 Python 库的报错信息,并整理了解决办法,供需要的小伙伴们参考。

问题提示

这里以上一篇《使用 Python 脚本实现图片相似度匹配》文中代码为例,首次执行时的报错信息如下:

1
2
3
4
5
penn@penn-VMware-Virtual-Platform:~/图片$ python3 1.py
Traceback (most recent call last):
File "/home/penn/图片/1.py", line 4, in <module>
import imagehash
ModuleNotFoundError: No module named 'imagehash'

根据报错信息,提醒找不到 imagehash 模块,使用 pip3 命令安装需要的模块,结果又出现了错误信息。这个信息表明正在尝试在一个由操作系统管理的 Python 环境中直接安装 Python 相关的包,为了保证系统 Python 环境的稳定性和安全性而采取限制措施:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
penn@penn-VMware-Virtual-Platform:~/图片$ pip3 install imagehash
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.

See /usr/share/doc/python3.13/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

解决方法

解决的方法有很多,这里推荐使用虚拟环境。因为使用虚拟环境可以避免直接修改系统的 Python 环境,同时方便管理依赖。按照提示创建一个虚拟环境,使用 python3 -m venv myenv 来创建虚拟环境,使用 source myenv/bin/activate 激活虚拟环境:

1
2
penn@penn-VMware-Virtual-Platform:~/图片$ python3 -m venv myenv
penn@penn-VMware-Virtual-Platform:~/图片$ source myenv/bin/activate

在激活虚拟环境后,使用以下命令安装所需的包。安装完成后运行命令 deactivate,退出虚拟环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(myenv) penn@penn-VMware-Virtual-Platform:~/图片$ pip install imagehash
Collecting imagehash
Downloading ImageHash-4.3.2-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting PyWavelets (from imagehash)
Downloading pywavelets-1.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.0 kB)
Collecting numpy (from imagehash)
Downloading numpy-2.2.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
Collecting pillow (from imagehash)
Downloading pillow-11.2.1-cp313-cp313-manylinux_2_28_x86_64.whl.metadata (8.9 kB)
Collecting scipy (from imagehash)
Downloading scipy-1.15.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)
Downloading ImageHash-4.3.2-py2.py3-none-any.whl (296 kB)
Downloading numpy-2.2.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.1/16.1 MB 21.3 MB/s eta 0:00:00
Downloading pillow-11.2.1-cp313-cp313-manylinux_2_28_x86_64.whl (4.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.6/4.6 MB 38.6 MB/s eta 0:00:00
Downloading pywavelets-1.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 38.6 MB/s eta 0:00:00
Downloading scipy-1.15.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (37.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 37.3/37.3 MB 41.4 MB/s eta 0:00:00
Installing collected packages: pillow, numpy, scipy, PyWavelets, imagehash
Successfully installed PyWavelets-1.8.0 imagehash-4.3.2 numpy-2.2.5 pillow-11.2.1 scipy-1.15.2

使用 Python 脚本实现图片相似度匹配

2025年9月16日 00:00

随着相机像素越来越大,图片体积也变大了。在图片处理中,较大的文件体积会影响性能,因此杜老师会先生成缩略图,筛选完成后再通过 Python 脚本实现图片相似度匹配。这里是一个简单的示例,供需要的小伙伴们参考。

脚本说明

以下是个基于 Python 的脚本,使用 PIL 以及 imagehash 库来实现。

遍历目录 A 中所有图片。

在目录 B 中查找相似的图片「通过感知哈希算法判断」

如找到匹配项,则将图片复制到目录 C,并以目录 A 图片的名字命名。

安装依赖

1
pip install pillow imagehash

注意:在运行脚本前,需安装所需的 Python 库。

脚本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import shutil
from PIL import Image
import imagehash

# 定义目录路径
dir_a = 'path/to/dirA'
dir_b = 'path/to/dirB'
dir_c = 'path/to/dirC'

# 设置相似度阈值(越小越严格)
threshold = 5

# 获取图片的感知哈希值
def get_image_hash(filepath):
try:
return imagehash.phash(Image.open(filepath))
except Exception as e:
print(f"无法处理文件 {filepath}: {e}")
return None

# 判断两个哈希值是否相似
def is_similar(hash1, hash2):
return hash1 - hash2 <= threshold

# 确保目标目录存在
os.makedirs(dir_c, exist_ok=True)

# 遍历目录 A
for filename in os.listdir(dir_a):
file_a_path = os.path.join(dir_a, filename)

# 检查是否为图片
if not filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
continue

hash_a = get_image_hash(file_a_path)
if hash_a is None:
continue

# 遍历目录 B 寻找相似图片
for b_filename in os.listdir(dir_b):
file_b_path = os.path.join(dir_b, b_filename)

# 检查是否为图片
if not b_filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
continue

hash_b = get_image_hash(file_b_path)
if hash_b is None:
continue

if is_similar(hash_a, hash_b):
# 构建目标路径
file_c_path = os.path.join(dir_c, filename)
# 复制并重命名文件
shutil.copy(file_b_path, file_c_path)
print(f"已找到匹配: {filename} -> {b_filename}, 已复制到 {file_c_path}")

注意:将 dir_a, dir_bdir_c 替换为实际路径;threshold 控制图像相似度阈值,可以根据需要调整;支持多种常见格式图片文件;使用 imagehash.phash 进行感知哈希的比较,适合用于识别视觉上接近的图片。

运行效果

1
2
3
4
5
(myenv) penn@penn-VMware-Virtual-Platform:~/图片$ python3 1.py
已找到匹配: image105.jpg -> 1745928332994.jpg, 已复制到 c/image105.jpg
已找到匹配: image001.jpg -> 1745736425856.jpg, 已复制到 c/image001.jpg
已找到匹配: image017.jpg -> 1745736425221.jpg, 已复制到 c/image017.jpg
已找到匹配: image085.jpg -> 1745928334851.jpg, 已复制到 c/image085.jpg

注意:脚本运行过程可能会有错误提示,需要根据提示进行修复。

❌