How do I convert Python Imaging Library code to PHP?

84 views Asked by At

I have recently been converting one of my Python projects into PHP so I can use it on my website but I have gotten stuck converting this bit.

Here is the Python code I'm trying to convert:

from PIL import Image, ImageTk
from random import randint

value = '00000000000000000000001100011000000000011000110000000000000000000000000000001000000000011000000000110000011000000011000000011111111100000000000000000000000000000000000000'

cmap = {'0': (255,255,255), '1': (0,0,0)}

data = [cmap[letter] for letter in value]
img = Image.new('RGB', (17, len(value)//17), "white")
img.putdata(data)
rand = str(randint(100000,999999))
path = "images/image" + rand + ".png"
img.save(path,"PNG")

This is what I have gotten so far in PHP:

$value = '00000000000000000000001100011000000000011000110000000000000000000000000000001000000000011000000000110000011000000011000000011111111100000000000000000000000000000000000000';

$cmap = str_replace(array('1', '0'), array('(255,255,255)', '(0,0,0)'), $value);

$data = // Does this loop through every number in $value?
$img = // In Python, this uses the Python Imaging Library. Would imagecreatefromstring($data) be usable?
// Add data to img variable
$rand = rand(100000,999999);
$path = "images/image" . $rand . ".png";
// Save image
0

There are 0 answers