2

SOLVED: Solution ------------------------------------------------------------------

I would like to execute the function getimagesize() from the PHP standard library in a Python script. The function must be passed an image path from Python.

I know how to run a php script in Python, but I can't figure out how to pass an argument from Python -> PHP and then get the result back into Python.

Here is what I have in the Python Script:

def php_script_runner(script_path):
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

Here is what I have in the PHP file (I am not sure how to pass an argument to it):

<?php getimagesize() ?>

How would I pass an argument to 'get_image_size.php' and have it return the result in Python for further processing?

I would like to further implement this idea for other custom PHP functions. The main goal of this question is not only to find the size of an image, but to be able to use this for other functions in the future that are php intensive.

The main goal of this is to determine:

  1. How to call a PHP script in Python
  2. How to send an agrument to a PHP script from Python
  3. How to parse the output of the PHP script back into Python
Community
  • 1
  • 1
kplus
  • 307
  • 1
  • 4
  • 16
  • 2
    http://www.php.net/manual/en/reserved.variables.argv.php You'd be better off see if there's a python library that does this, rather than the rather massive overhead of having to fire up an external php process just to do this one thing. – Marc B Mar 06 '13 at 15:48
  • @MarcB Already stated in the comment below. – kplus Mar 06 '13 at 15:53
  • possible duplicate of [How to obtain image size using standard Python class (without using external library)?](http://stackoverflow.com/questions/8032642/how-to-obtain-image-size-using-standard-python-class-without-using-external-lib) – Marc B Mar 06 '13 at 15:59
  • @MarcB No, this is not a duplicate. The question ask more than just how to get an image size in Python. Read it again if you think otherwise. – kplus Mar 06 '13 at 16:04
  • @MarcB Almost have it working using http://www.php.net/manual/en/reserved.variables.argv.php. Thank you! – kplus Mar 06 '13 at 16:18
  • @MarcB Got it reading in the array, Thanks! – kplus Mar 06 '13 at 16:51

3 Answers3

3

What's wrong with using the Python Image Library, PIL ?

from PIL import Image
im=Image.open(filepath)
print im.size # (height,width) tuple

Invoking a PHP script to get the image dimensions, is just plain wrong. I can't really think of anything that PHP does better than Python, that it justifies firing up an instance of both runtimes :)

My advice: Stick with one language at a time, and learn the standard library of Python :) You shouldn't be having trouble doing something in Python if you can do it in PHP.

Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • Like I said multiple times, this is just a stepping stone. I am well aware of PIL, I know the standard library and PIL is not part of it. I have my reasons for invoking PHP. – kplus Mar 06 '13 at 16:29
  • 2
    Well you also said that there was no way to get image dimensions from Python. That was why I suggested PIL. Sorry for that :) – Morten Jensen Mar 06 '13 at 16:31
  • Thank you for your input! I made it more clear with the latest edit. – kplus Mar 06 '13 at 16:35
3

Solved by doing this: Python:

import subprocess
import json
import sys
import os

def php(script_path, argument):
    p = subprocess.Popen(['php', script_path, argument], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

image_dimensions_json =str(php("get_image_size.php", image_location_relative[1:]))
dic = json.loads(image_dimensions_json)
print str(dic["0"]) + "|" + str(dic["1"])

The reason for the [1:] is because for some reason the "/" in the path causes an error. I found removing it fixed the issue.

PHP:

<?php

echo(json_encode(getimagesize($argv[1])));

?>

echo or print_r work. I found it easy to convert the array to JSON, as Python has a json decoder built into the standard library.

Hope this helps people! I plan on using this conecpt to implement much larger PHP functions.

kplus
  • 307
  • 1
  • 4
  • 16
  • This works extremely well for other functions and can easily be modified to parse output and integrate into php frameworks. – kplus Mar 08 '13 at 15:54
2

If I where you, I'd echo the Python result out as a JSON (or possibly XML) object and then reread that information with the PHP script. If at all possible I'd highly recommend separating these two languages into two separate webpages and maybe even get the Python to return the data via a GET style query to the page, aka API style.

Phillip Berger
  • 2,317
  • 1
  • 11
  • 30
  • 2
    or just see if python has something similar (which it almost certainly does), saving the overhead of having to fire up PHP just to do something python already could anyways. – Marc B Mar 06 '13 at 15:51
  • I'm not running this script on a webpage. I am familiar with Python, but it does not have a built-in module for getting image size. I happen to know that PHP does have a great function for it, so why not use it? (There are also a few other PHP functions that I have written which I would like to incorporate into Python) – kplus Mar 06 '13 at 15:52
  • @MarcB: Good answer. We meet again! – Phillip Berger Mar 06 '13 at 15:54
  • 2
    @SensulPanda: firing up PHP from within python just to run a single function is like driving 500 miles to buy something at a store that's available in your kitchen cupboard already. – Marc B Mar 06 '13 at 15:58
  • @MarcB: I love your comment! – Phillip Berger Mar 06 '13 at 15:59
  • Like I said above, I plan on doing it for other functions I have written in PHP. This is for a much larger goal, but I must start somewhere. – kplus Mar 06 '13 at 16:00