SFPG: Difference between revisions

From Geitenpad
Jump to navigation Jump to search
 
m 1 revision imported
 
(No difference)

Latest revision as of 14:34, 5 January 2025

Olimex

root@a20-OLinuXino:~# aptitude install imagemagick
root@a20-OLinuXino:~# aptitude install python-imaging
root@a20-OLinuXino:~# touch /var/log/preCacheSFPG.log
root@a20-OLinuXino:~# vim /etc/crontab
  • Add line
0     4 * * *   root /root/scripts/preCacheSFPG.py > /var/log/preCacheSFPG.log
  • Create script
root@a20-OLinuXino:~# vim /root/scripts/preCacheSFPG.py
#!/usr/bin/python

import os
import sys
import Image
import time
import datetime 

preCachSize = 40000
albumfiles = []
result = []
sfpg = '/var/www/html/sfpg/'
cache = sfpg + 'sfpg_data/'
albums = '/home/rijk/Documents/Pictures/'
info = cache + 'info/'
infofile = '.sfpg'
thumb = cache + 'thumb/'
preview = cache + 'preview/'
thumb_size = 120
preview_size = 800 

def preCache(original, cachedFile, newSize):
       dirname = os.path.dirname(cachedFile)
       if not os.path.exists(dirname):
               os.makedirs(dirname)
       command = 'convert -strip -auto-orient "' + original + '" -resize "%dx%d" "' + cachedFile + '"'
       command = command % (newSize, newSize)
       os.system(command)
       print 'added: ' + time.asctime() + ' ' + cachedFile

def writeInfo(original, infoFile):
       dirname = os.path.dirname(infoFile)
       if not os.path.exists(dirname):
               os.makedirs(dirname)
       creationtime = getCreationTime(original)
       filesize = '%.2f'%(os.path.getsize(original)/(1024.0*1024))
       imagesize = Image.open(original).size
       file = open(infoFile, 'w')
       file.write(creationtime + '|' + filesize + ' MB|%d|%d|'%imagesize)
       file.close()

def getCreationTime(imagePath):
       creationtime = time.strftime('%d-%m-%Y %H:%M:%S', time.gmtime(os.path.getmtime(imagePath)))
       imageFile = Image.open(imagePath)
       if hasattr(imageFile, '_getexif'):
               exifdata = imageFile._getexif()
               if exifdata:
                       if 0x9003 in exifdata:
                               ctime = exifdata[0x9003].replace('\x00', )
                               if ctime == '0000:00:00 00:00:00':
                                       return creationtime
                               else:   
                                       try:
                                               ctime = datetime.datetime.strptime(ctime, '%Y:%m:%d %H:%M:%S')
                                               creationtime = time.strftime('%d-%m-%Y %H:%M:%S', ctime.timetuple())
                                       except: 
                                               ctime = datetime.datetime.strptime(ctime[0:19], '%Y-%m-%d %H:%M:%S')
                                               creationtime = time.strftime('%d-%m-%Y %H:%M:%S', ctime.timetuple())
       return creationtime

# Add photo's to the list if no preview file exists
for root, subFolders, files in os.walk(albums, followlinks=True):
   for file in files:
               albumfile = os.path.join(root, file)
               if not os.path.exists(albumfile.replace(albums, preview, 1)):
                       albumfiles.append(albumfile)

# Only precache jpg
for albumfile in albumfiles:
       if (albumfile.endswith('jpg') or albumfile.endswith('JPG')):
               result.append(albumfile)

# Just a few            
for uncachedFile in result[0:preCachSize]:
       preCache(uncachedFile, uncachedFile.replace(albums, thumb, 1), thumb_size)
       preCache(uncachedFile, uncachedFile.replace(albums, preview, 1), preview_size)
       writeInfo(uncachedFile, uncachedFile.replace(albums, info, 1) + infofile)

# Set owner of precaches to wwwrun              
os.system('chown -R www-data:www-data ' + cache)

# Remove old thumbs
for root, subFolders, files in os.walk(thumb, followlinks=False):
   for file in files:
               thumbfile = os.path.join(root, file)
               if not os.path.exists(thumbfile.replace(thumb, albums, 1)):
                       print 'remove ' + thumbfile
                       os.remove(thumbfile)

# Remove old previews
for root, subFolders, files in os.walk(preview, followlinks=False):
   for file in files:
               previewfile = os.path.join(root, file)
               if not os.path.exists(previewfile.replace(preview, albums, 1)):
                       print 'remove ' + previewfile
                       os.remove(previewfile)

# Remove old info
for root, subFolders, files in os.walk(info, followlinks=False):
   for file in files:
               infofile = os.path.join(root, file)
               if not file == '_info.sfpg' and not os.path.exists(infofile.replace(info, albums, 1).replace('.sfpg', )):
                       print 'remove ' + infofile
                       os.remove(infofile)
root@a20-OLinuXino:~# chmod +x /root/scripts/preCacheSFPG.py

ProBook 6550b

  • Exactly the same as Olimex, only /var/www/html/sfpg instead of /var/www/sfpg
root@ppl:/var/www/html/sfpg# tree
.
├── favicon.ico
├── index.php
├── index.php.beforeresizing
├── index.php.only.login
├── readme.txt
└── sfpg_data