mirror of https://github.com/helloxz/imgurl
增加批量压缩脚本
parent
953c981e3c
commit
7713f704c6
10
README.md
10
README.md
|
@ -1,5 +1,5 @@
|
|||
# ImgURL
|
||||
ImgURL是一款简单、好用的图床程序,使用PHP + SQLite 3开发,不需要复杂的配置,开箱即用。
|
||||
ImgURL是一款简单、纯粹的图床程序,使用PHP + SQLite 3开发。
|
||||
|
||||
![](https://i.bmp.ovh/imgs/2018/12/06cf0ac3b7625b6b.png)
|
||||
|
||||
|
@ -24,7 +24,8 @@ ImgURL是一款简单、好用的图床程序,使用PHP + SQLite 3开发,不
|
|||
* pathinfo
|
||||
|
||||
### 安装
|
||||
请参考帮助文档:[https://doc.xiaoz.me/](https://doc.xiaoz.me/#/imgurl2/)
|
||||
* 常规安装请参考帮助文档:[https://doc.xiaoz.me/](https://doc.xiaoz.me/#/imgurl2/)
|
||||
* 宝塔面板安装ImgURL参考:[https://www.xiaoz.me/archives/12081](https://www.xiaoz.me/archives/12081)
|
||||
|
||||
### Demo
|
||||
* [http://test.imgurl.org/](http://test.imgurl.org/)
|
||||
|
@ -35,11 +36,16 @@ ImgURL是一款简单、好用的图床程序,使用PHP + SQLite 3开发,不
|
|||
![](https://www.xiaoz.me/wp-content/uploads/2013/12/juanzeng260.png)
|
||||
|
||||
### 鸣谢
|
||||
|
||||
ImgURL的诞生离不开以下项目,在此表示感谢。
|
||||
|
||||
* [LayUI](https://github.com/sentsin/layui)
|
||||
* [CodeIgniter](https://github.com/bcit-ci/CodeIgniter)
|
||||
* [clipBoard.js](https://github.com/baixuexiyang/clipBoard.js)
|
||||
* [Parsedown](https://github.com/erusev/parsedown)
|
||||
* [jQuery](https://github.com/jquery/jquery)
|
||||
* [tinypng](https://tinypng.com/)
|
||||
* [ModerateContent](https://www.moderatecontent.com/)
|
||||
|
||||
### 联系我
|
||||
* Blog:[https://www.xiaoz.me/](https://www.xiaoz.me/)
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
##### name: 图片批量压缩 #####
|
||||
##### author: xiaoz.me #####
|
||||
##### update: 2018-03-13 #####
|
||||
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
|
||||
#图片根目录
|
||||
global imgpath
|
||||
#设置图片绝对路径
|
||||
imgpath = '/data/wwwroot/test.imgurl.org'
|
||||
conn = sqlite3.connect(imgpath + "/data/imgurl.db3")
|
||||
|
||||
|
||||
c = conn.cursor()
|
||||
#查询10张没有压缩的图片
|
||||
imgs = c.execute("SELECT a.path,a.compression,b.mime,b.ext,a.id FROM img_images AS a INNER JOIN img_imginfo b ON a.imgid = b.imgid AND a.compression = 0 ORDER BY a.id DESC LIMIT 10")
|
||||
|
||||
#更新图片状态,compression标记为1,标识已经压缩
|
||||
def upimg(imgid):
|
||||
u = conn.cursor()
|
||||
#更新数据库
|
||||
upimg = u.execute("UPDATE img_images SET compression = 1 WHERE `id` = " + str(imgid))
|
||||
conn.commit()
|
||||
|
||||
#压缩图片的函数
|
||||
def compress(mime,path,imgid):
|
||||
#图片完整路径
|
||||
fullpath = imgpath + path
|
||||
#图片大小
|
||||
imgsize = os.path.getsize(fullpath)
|
||||
#如果图片大于100K就进行压缩
|
||||
if imgsize >= 102400:
|
||||
if mime == 'image/jpeg':
|
||||
#优化图片
|
||||
os.system('jpegoptim -m 80 ' + fullpath)
|
||||
#更新数据库
|
||||
upimg(imgid)
|
||||
print(path + "已优化!\n")
|
||||
elif mime == 'image/png' or mime == 'image/bmp':
|
||||
#优化图片
|
||||
os.system('optipng ' + fullpath)
|
||||
#更新数据库
|
||||
upimg(imgid)
|
||||
print(path + "已优化!\n")
|
||||
else:
|
||||
skip = c.execute("UPDATE img_images SET compression = -1 WHERE `id` = " + str(imgid))
|
||||
print("当前没有需要处理的图片!\n")
|
||||
conn.commit()
|
||||
else:
|
||||
print("图片不足100k,跳过!\n")
|
||||
skip = conn.execute("UPDATE img_images SET compression = -1 WHERE `id` = " + str(imgid))
|
||||
conn.commit()
|
||||
|
||||
#遍历输出
|
||||
for row in imgs:
|
||||
compress(row[2],row[0],row[4])
|
||||
|
||||
#关闭数据库连接
|
||||
conn.close()
|
|
@ -1 +1 @@
|
|||
v2.0-20190110
|
||||
v2.01-20190313
|
Loading…
Reference in New Issue