How To Speed Up Your Website With Kraken.io

Posted by in Articles, Hacks, Programming, Python

If you are looking to find best solution to speed up your website, then you are at right place. In this tutorial we will demonstrate how to speed up your website by compressing images without loosing quality.

Follow steps below:

1. Create kraken.io account .

2. Create python script that using kraken.io  API to upload image, compress it and download(overwrite ) it

 

# -- coding: utf-8 --
from krakenio import Client
import os 
 
def find_images(path):
	images_list = []
	for root, dirs, files in os.walk(path):
	    for name in files:
	        if name.endswith(('.jpg', '.jpeg','.png')):
	        	full_path = os.path.join(root, name)
	        	if full_path not in images_list:
	        		images_list.append(full_path)
	return images_list
 
def upload_to_kraken(api, data, path):
	try:
		result = api.upload(path, data);
 
		if result.get('success'):
		    os.system('wget -q  ' + result.get('kraked_url')+' -O ' + path)
		    print 'Download ' + result.get('kraked_url') + 'to ' + path
 
		else:
		    print result.get('message')
	except:
		print 'Error!'
 
api = Client('YOUR API KEY', 'YOUR SECRET KEY')
data = {
    'wait': True,
    'lossy': True,
    'quality': 75
}
path = '/home/john155/webapps/hackspc/images'
 
images_list = find_images(path)
for one_image in images_list:
	upload_to_kraken(api, data, one_image)

 

I called this script kraken_optimizer.py

To make this script working, you need to add your API key and SECRET KEY from kraken.io account .

3. Update python script with your API KEY and SECRET KEY

api = Client(‘YOUR API KEY’, ‘YOUR SECRET KEY’)

4. Update python script with your images path

path = ‘/home/john155/webapps/hackspc/images’

5. Execute kraken_optimizer.py . You can run it on every server that support python , just execute command :

python kraken_optimizer.py

kraken_optimizer.py will optimize recursively all images with format ‘.jpg’, ‘.jpeg’,’.png’ from directory that is specified in variable called path.