Skip to content

Commit 2d21191

Browse files
author
Zsolt Gál
committed
Initial commit
1 parent 9aa14f8 commit 2d21191

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.phar
44
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock
7+
.idea/

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# php-cli-iterm-image-renderer
22
Render images with PHP in your iterm2 console
3+
4+
## usage
5+
6+
```
7+
#!/usr/bin/env php
8+
<?php
9+
10+
use Technodelight\ITermImage\Image;
11+
$image = Image::fromUri('https://picsum.photos/200', 200);
12+
if (!empty($image)) {
13+
echo $image;
14+
}
15+
```
16+
Running the above script will render a random picsum image in your terminal, 200 px wide.
17+
18+
## requirements
19+
20+
This library was built to work with the famous iTerm2 OS X application.
21+
It has a basic fallback version to render the URI instead of the image, if the current
22+
iTerm does not support displaying images (or you're not using iTerm2 at all).

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "technodelight/php-cli-iterm-image-renderer",
3+
"description": "Render images with PHP in your iterm2 console",
4+
"type": "library",
5+
"require": {
6+
"technodelight/shell-exec": "^1.0"
7+
},
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Zsolt Gál",
12+
"email": "zsolt.gal@sessiondigital.com"
13+
}
14+
],
15+
"autoload": {
16+
"PSR-4": {"Technodelight\\ITermImage\\": "src/"}
17+
}
18+
}

composer.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ITermVersion.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Technodelight\ITermImage;
4+
5+
use Technodelight\ShellExec\Command;
6+
use Technodelight\ShellExec\Exec;
7+
8+
class ITermVersion
9+
{
10+
private $parsed = false;
11+
private $version;
12+
13+
public function __toString()
14+
{
15+
return $this->guessVersion();
16+
}
17+
18+
private function guessVersion()
19+
{
20+
if ($this->parsed) {
21+
return $this->version;
22+
}
23+
24+
$this->parsed = true;
25+
if ($version = getenv('TERM_PROGRAM_VERSION')) {
26+
return $this->version = $version;
27+
} else if (is_file('/Applications/iTerm.app/Contents/Info.plist')) {
28+
$cmd = Command::create('cat')
29+
->withArgument('/Applications/iTerm.app/Contents/Info.plist')
30+
->pipe(
31+
Command::create('grep')
32+
->withArgument('CFBundleVersion')
33+
->withShortOption('A1')
34+
);
35+
$shell = new Exec();
36+
$out = $shell->exec($cmd);
37+
$xml = simplexml_load_string('<root>' . join('', $out) . '</root>');
38+
return $this->version = (string) $xml->string;
39+
}
40+
41+
return $this->version = '';
42+
}
43+
}

src/Image.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Technodelight\ITermImage;
4+
5+
class Image
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $uri;
11+
/**
12+
* @var string
13+
*/
14+
private $contents;
15+
/**
16+
* @var int
17+
*/
18+
private $thumbnailWidth;
19+
/**
20+
* @var ITermVersion
21+
*/
22+
private $itermVersion;
23+
24+
public static function fromUri($uri, $thumbWidth = 300, ITermVersion $itermVersion = null)
25+
{
26+
$instance = new self;
27+
$instance->uri = $uri;
28+
$instance->thumbnailWidth = $thumbWidth;
29+
$instance->itermVersion = $itermVersion ?: new ITermVersion;
30+
31+
return $instance;
32+
}
33+
34+
public static function fromContents($contents, $thumbWidth = 300, ITermVersion $itermVersion = null)
35+
{
36+
$instance = new self;
37+
$instance->contents = $contents;
38+
$instance->thumbnailWidth = $thumbWidth;
39+
$instance->itermVersion = $itermVersion ?: new ITermVersion;
40+
41+
return $instance;
42+
}
43+
44+
public function render()
45+
{
46+
try {
47+
if (!$this->isItermCapable()) {
48+
return $this->uri;
49+
}
50+
51+
return $this->renderThumbnail($this->getImageContents());
52+
} catch (\Exception $e) {
53+
return '';
54+
}
55+
}
56+
57+
public function __toString()
58+
{
59+
return $this->render();
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
protected function getImageContents()
66+
{
67+
if (!empty($this->contents)) {
68+
return $this->contents;
69+
}
70+
71+
return file_get_contents($this->uri);
72+
}
73+
74+
/**
75+
* @param $contents
76+
* @return string
77+
*/
78+
protected function renderThumbnail($contents)
79+
{
80+
return chr(27) .
81+
']1337;File=inline=1;width=' . $this->thumbnailWidth . 'px;preserveAspectRatio=1:'
82+
. base64_encode($contents)
83+
. chr(7);
84+
}
85+
86+
private function isItermCapable()
87+
{
88+
return version_compare((string) $this->itermVersion, '3.0.0', '>=');
89+
}
90+
91+
private function __construct()
92+
{
93+
}
94+
}

0 commit comments

Comments
 (0)