Skip to content

Commit 4950c8e

Browse files
committed
add the GD example
1 parent 928802a commit 4950c8e

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tmp/*

builtin/core.tea

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public NL = "\n"
1111
#php empty(expr Any) Bool
1212
#php var_dump(expr0 Any, expr1 Any = #default, expr2 Any = #default, expr3 Any = #default)
1313
#php print_r(expr, is_return Bool = #default)
14+
#php header(info String, replace = true, http_response_code Int = #default)
1415

1516
// php builtin constants
1617
#php __DIR__ String
@@ -34,6 +35,7 @@ public NL = "\n"
3435
#php ErrorException: Exception {}
3536

3637
// file
38+
#php file_exists(filename String) Bool
3739
#php dirname(path String, levels UInt = #default) String
3840
#php basename(path String, suffix String = #default) String
3941
#php realpath(path String) String

dist/examples/__public.th

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44

55
public M_PI Float
66

7+
#php imagecreatetruecolor(width Int, height Int) Any
8+
9+
#php imagecolorallocatealpha(image Any, r Int, g Int, b Int, a Int) Int
10+
11+
#php imagefill(image Any, x Int, y Int, color Int) Bool
12+
13+
#php imagerotate(image Any, angle Float, background_color Int) Any
14+
15+
#php imagesx(image Any) Int
16+
17+
#php imagesy(image Any) Int
18+
19+
#php imagettfbbox(size Float, angle Float, fontfile String, text String) Int.Array
20+
21+
#php imagettftext(image Any, size Float, angle Float, x Int, y Int, color Int, fontfile String, text String) Int.Array
22+
23+
#php imagepng(image Any, to String = none, quality Int = -1, filters Int = #default) Bool
24+
25+
#php imagedestroy(image Any)
26+
727
#php PDOStatement {
828
fetch(fetch_style Int = #default) String.Dict
929
fetchAll(fetch_style Int = #default) String.Dict.Array

dist/examples/gd.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace tea\examples;
3+
4+
require_once __DIR__ . '/__unit.php';
5+
6+
// ---------
7+
$tmp_path = dirname(__DIR__, 2) . '/tmp/';
8+
$fontfile = $tmp_path . 'SourceHanSansSC-Regular.otf';
9+
if (!file_exists($fontfile)) {
10+
echo "The font file '{$fontfile}' not found.\nPlease download it and put to path {$fontfile}", NL;
11+
exit;
12+
}
13+
14+
$w = 629;
15+
$h = $w;
16+
$im = imagecreatetruecolor($w, $h);
17+
18+
$black = imagecolorallocatealpha($im, 0, 0, 0, 0);
19+
$non_color = imagecolorallocatealpha($im, 0, 0, 0, 127);
20+
$red = imagecolorallocatealpha($im, 255, 0, 0, 0);
21+
22+
imagefill($im, 0, 0, $red);
23+
24+
$im = imagerotate($im, 45, $non_color);
25+
$w = imagesx($im);
26+
$h = imagesy($im);
27+
28+
$size = 310;
29+
$angle = 0;
30+
$text = '';
31+
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
32+
$dx = $bbox[2] + $bbox[0];
33+
$dy = (int)abs($bbox[5] + $bbox[1]);
34+
imagettftext($im, $size, $angle, intval((($w - $dx) / 2)), $dy + intval((($h - $dy) / 2)), $black, $fontfile, $text);
35+
36+
$size = 30;
37+
$text = '新春快乐';
38+
$bbox = imagettfbbox($size, 0, $fontfile, $text);
39+
$dx = $bbox[2] + $bbox[0];
40+
$dy = (int)abs($bbox[5] + $bbox[1]);
41+
imagettftext($im, $size, 0, intval((($w - $dx) / 2)), $h - $dy - 130, $black, $fontfile, $text);
42+
43+
$size = 10;
44+
$text = 'tealang';
45+
$bbox = imagettfbbox($size, 0, $fontfile, $text);
46+
$dx = $bbox[2] + $bbox[0];
47+
$dy = (int)abs($bbox[5] + $bbox[1]);
48+
imagettftext($im, $size, 0, intval((($w - $dx) / 2)), $h - $dy - 100, $black, $fontfile, $text);
49+
50+
$target_file = $tmp_path . 'chun.png';
51+
$result = imagepng($im, $target_file);
52+
imagedestroy($im);
53+
54+
if ($result) {
55+
echo 'Image ' . $target_file . ' generated.', NL;
56+
}
57+
else {
58+
echo 'Image ' . $target_file . ' generate failure.', NL;
59+
}
60+
// ---------
61+
62+
// program end

examples/gd.tea

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#main
2+
3+
#php imagecreatetruecolor(width Int, height Int) Any
4+
#php imagecolorallocatealpha(image, r Int, g Int, b Int, a Int) Int
5+
#php imagefill(image, x Int, y Int, color Int) Bool
6+
#php imagerotate(image, angle Float, background_color Int) Any
7+
#php imagesx(image) Int
8+
#php imagesy(image) Int
9+
#php imagettfbbox(size Float, angle Float, fontfile String, text String) Int.Array
10+
#php imagettftext(image, size Float, angle Float, x Int, y Int, color Int, fontfile String, text String) Int.Array
11+
#php imagepng(image, to String = none, quality = -1, filters Int = #default) Bool
12+
#php imagedestroy(image)
13+
14+
var tmp_path = dirname(__DIR__, 2) concat '/tmp/'
15+
var fontfile = tmp_path concat 'SourceHanSansSC-Regular.otf'
16+
if not file_exists(fontfile) {
17+
echo "The font file '$fontfile' not found.\nPlease download it and put to path $fontfile"
18+
exit
19+
}
20+
21+
var w Int = 629
22+
var h = w
23+
var im = imagecreatetruecolor(w, h)
24+
25+
var black = imagecolorallocatealpha(im, 0, 0, 0, 0)
26+
var non_color = imagecolorallocatealpha(im, 0, 0, 0, 127)
27+
var red = imagecolorallocatealpha(im, 255, 0, 0, 0)
28+
29+
imagefill(im, 0, 0, red)
30+
31+
im = imagerotate(im, 45, non_color)
32+
w = imagesx(im)
33+
h = imagesy(im)
34+
35+
// the main text
36+
var size = 310
37+
var angle = 0
38+
var text = ''
39+
var bbox = imagettfbbox(size, angle, fontfile, text)
40+
var dx = bbox[2] + bbox[0]
41+
var dy = abs(bbox[5] + bbox[1]) as Int
42+
imagettftext(im, size, angle, ((w - dx) / 2).int(), dy + ((h - dy) / 2).int(), black, fontfile, text)
43+
44+
// the subtext
45+
size = 30
46+
text = '新春快乐'
47+
bbox = imagettfbbox(size, 0, fontfile, text)
48+
dx = bbox[2] + bbox[0]
49+
dy = abs(bbox[5] + bbox[1]) as Int
50+
imagettftext(im, size, 0, ((w - dx) / 2).int(), h - dy - 130, black, fontfile, text)
51+
52+
// the creator
53+
size = 10
54+
text = 'tealang'
55+
bbox = imagettfbbox(size, 0, fontfile, text)
56+
dx = bbox[2] + bbox[0]
57+
dy = abs(bbox[5] + bbox[1]) as Int
58+
imagettftext(im, size, 0, ((w - dx) / 2).int(), h - dy - 100, black, fontfile, text)
59+
60+
var target_file = tmp_path concat 'chun.png'
61+
result = imagepng(im, target_file)
62+
imagedestroy(im)
63+
64+
if result {
65+
echo 'Image $target_file generated.'
66+
}
67+
else {
68+
echo 'Image $target_file generate failure.'
69+
}
70+

tmp/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# The temporary directory

0 commit comments

Comments
 (0)