Editorial Workflows

Upload image to WordPress

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: A workflow that uses xmlrpc to scale and upload images directly to a WordPress blog. You can choose to name the image, resize or use the original size. The image will upload to the Wordpress image library, create thumbnails and link articles automatically. A markdown image link link will be created and the selected text is used as alt text for the image.

More info at: http://www.jackenhack.com

Shared by: http://www.jackenhack.com

Comments: Comment Feed (RSS)

Jacken — 31 Jan 2015
A newer version with lots of improvements here: http://www.editorial-workflows.com/workflow/6357181481877504/sSCTFSH_QcI

+ Add Comment

Workflow Preview
Username ?
Variable Name
username
Value
jacken
Set Variable ?
Variable Name
SiteURL
Value
http://www.jackenhack.com/xmlrpc.php
Set Variable ?
Variable Name
password
Value
password
Check modules Installed ?
Source Code
#coding: utf-8
import workflow
import os, sys

#sys.path += [os.path.join(os.getcwd(), 'pypi-modules')]
sys.path += [os.path.join(os.getcwd(), '../../../Documents/pypi-modules')]
import imp
try:
    imp.find_module('wordpress_xmlrpc')
    workflow.set_output('True')
except ImportError:
    workflow.set_output('False')
if missing module... ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
True
Missing module alert ?
Title
Missing Module
Message
You need to install the wordpress_xmlrpc module first.
Button 1
Instructions
Output Value
instruction
Button 2
(don't show)
Output Value
Button 3
(don't show)
Output Value
Show Cancel Button
ON
…End If
If instruction button pressed, go to instructions ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
instruction
Open URL ?
Open in
  • In-App Browser
  • Default App / Safari
URL
http://www.jackenhack.com/editorial-app-import-modules-with-pipista/
Tab
  • Last-used Tab
  • New Tab
  • Tab with ID:
Unique identifier
Wait until Loaded
OFF
Reveal Browser Automatically
ON
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
OFF
Message
Stopped
…End If
if nothing selected... ?
Run the block if
Selected Text
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Show HUD ?
HUD Text
Select Alt text for image first!
Duration
  • 1 Second
  • 2 Seconds
  • 3 Seconds
Icon
  • "Success"
  • "Error"
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
ON
Message
Stopped
…End If
Python scale and upload to WP ?
Source Code
#coding: utf-8
import workflow
#import ui

import sys, os, photos, console, clipboard, Image
#sys.path += [os.path.join(os.getcwd(), '../Commands/pypi-modules')]
sys.path += [os.path.join(os.getcwd(), '../../../Documents/pypi-modules')]
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
from cStringIO import StringIO
from PIL import Image
import pickle

prefsfile = 'prefs.pkl'
DEFAULTWIDTH = 600
imagequality = 85

def readprefs(filename):
	if os.path.isfile(filename):
		input = open(filename, 'r')
		width = pickle.load(input)
		input.close()
	else:
		width = DEFAULTWIDTH
	return width
	
def writeprefs(filename, width):
	output = open(filename, 'wb')
	# Pickle dictionary using protocol 0.
	pickle.dump(width, output)
	output.close()

def scaleimage(img, newwidth):
	if img.size[0] < newwidth:
		# dont upscale small image
		console.alert('Image size', 'Image is smaller than ' + str(newwidth) + ' px wide.', 'OK')
		return img
	wpercent = (newwidth/float(img.size[0]))
	hsize = int((float(img.size[1])*float(wpercent)))
	img = img.resize((newwidth,hsize), Image.ANTIALIAS)
	return img

username = workflow.get_variable('username')
site = workflow.get_variable('SiteURL')
password = workflow.get_variable('password')
if not username:
	console.alert('Missing Username', 'Set the username in the script variable of this workflow.')
	workflow.stop()
if not site:
	console.alert('Missing SiteURL', 'Set the site URL to the xmlpc.php in the script variable of this Workflow')
	workflow.stop()

if not password:
	console.alert('Missing password', 'Set the password in the script variable of this Workflow')
	workflow.stop()

prefs = {}
console.clear()
selphoto = photos.pick_image(show_albums=True)
if not selphoto:
	console.hud_alert('No photo selected!', 'error', 2)
	workflow.stop()
width = readprefs(prefsfile)
shouldresize = console.alert('Image Size' + str(selphoto.size[0]) + 'x' + str(selphoto.size[1]) + '.', 'Do you want to resize?', 'Original', 'Resize')
if shouldresize == 2:
	newwidth = console.input_alert('Current image width ' + str(selphoto.size[0]) + ', resize to', '', str(width), 'Scale')
	newwidth = int(newwidth)
	if newwidth != width:
		# User changed the width, save for next time in preference file
		width = newwidth
		writeprefs(prefsfile, width)

# Ask for file name
filename = console.input_alert('filename', '', '', 'Save')
if not filename:
	print 'No filename given.'
	workflow.stop()
# check for file extension, add if missing
if filename[-4:] != '.jpg':
	filename = filename + '.jpg'
if shouldresize == 'Resize':
	selphoto = scaleimage(selphoto, int(width))
imgfile = StringIO()
selphoto.save(imgfile, format='JPEG', quality = imagequality)
imagestring = imgfile.getvalue()

# prepare metadata
data = {
        'name': filename,
        'type': 'image/jpg',  # mimetype
}

console.show_activity()

# read the binary file and let the XMLRPC library encode it into base64
data['bits'] = xmlrpc_client.Binary(imagestring)

try:
	client = Client(site, username, password)
	response = client.call(media.UploadFile(data))
except Exception as e:
	print e
	workflow.stop()

# response == {
#       'id': 6,
#       'file': 'picture.jpg'
#       'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
#       'type': 'image/jpg',
# }
attachment_id = response['id']

console.hide_activity()
workflow.set_output(response['url'])
Replace Selected Text ?
Replacement Text
![Selected Text](Input)