-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_wed_card.php
190 lines (160 loc) · 6.71 KB
/
create_wed_card.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" );
include 'config/config.php';
error_reporting(E_ALL);
extract($_REQUEST);
$sql = "SELECT card_bg_path, card_settings FROM " . TBL_CARDS . " WHERE card_id = {$item_id}";
$card_data = mysql_query($sql) or die (" $sql <br/>". mysql_error());
if ( !mysql_num_rows( $card_data ) ) { die('wrong data'); }
$card_data = mysql_fetch_object( $card_data );
$card_settings = unserialize( $card_data->card_settings );
$positions = explode( '_', $card_settings[0]['content_container_position'] );
$original_card = BLANK_CARDS . "$card_data->card_bg_path";
/* get the image extension and create image accordingly */
$ext = strtolower( end( explode( '.', $original_card ) ) );
if ($ext == 'jpg' || $ext == 'jpeg' ) {
$original_card = imagecreatefromjpeg( $original_card );
} else if ( $ext == 'png' ) {
$original_card = imagecreatefrompng( $original_card );
} else if ( $ext == 'gif' ) {
$original_card = imagecreatefromgif( $original_card );
} else {
die('wrong type');
}
/* define variables */
$original_txt = $original_txt_br = $temp = $couple = array();
/* save original text and exploded array */
foreach($texts as $tt) {
$original_txt[] = nl2br(urldecode( $tt ) );
$original_txt_br[] = explode('<br />', nl2br( urldecode( $tt ) ) );
}
/* 0 => font-style, 1 => font-size, 2 => color, 3 => align */
foreach( $main as $mt ) {
$temp[] = urldecode( $mt );
}
$main = $temp;
unset($temp);
/* 0 => font-style, 1 => font-size, 2 => color, 3 => align */
foreach( $couples as $ct ) {
$couple[] = urldecode( $ct );
}
/* count total rows */
$total_box_1 = count($original_txt_br[0]); /* first text block */
$total_box_2 = count($original_txt_br[1]); /* bride / groom text block */
$bride_row = -1;
$groom_row = -1;
if ( !empty( $original_txt_br[1][0] ) ) { /* means there is not bride / groom text block */
/* bride row will be at first text block count */
$bride_row = (int)$total_box_1;
/* groom row either next to bride_row or next + 1 (AND) row */
$groom_row = $total_box_2 > 2 ? (int)$bride_row + 2 : (int)$bride_row + 1;
}
/* implode the original text by <br /> (nl2br) generate <br /> tag and then explode it with the same */
$txt_process = explode( '<br />', implode('<br />', $original_txt ) );
/* remove extra spaces from each line and get each line ttfbox width to be use for imagecreatetruecolor */
$counter = $bride_groom_h = $y = 0;
foreach ($txt_process as $tp) {
$temp[] = trim($tp);
if ( $counter == $bride_row || $counter == $groom_row ) {
$font_style = FONTS_PATH . strtolower( $couple[0] );
$font_size = $couple[1];
} else {
$font_style = FONTS_PATH . strtolower( $main[0] );
$font_size = $main[1];
}
$width_box = imagettfbbox($font_size, 0, $font_style, trim($tp));
$txtWidth[] = abs( $width_box[4]-$width_box[6] );
if ( $counter == 0 ) {
/* default y axis for the text */
$y = abs( $width_box[7]-$width_box[1] );
} else if ( $counter == $bride_row ) {
$bride_groom_h = abs( $width_box[7]-$width_box[1] );
}
$counter++;
}
$tWidth = max($txtWidth);
$txt_process = $temp;
unset($temp);
$w = $tWidth > $w ? $tWidth : $w;
/* create true color image with the provided height and calculated width */
$card = imagecreatetruecolor( $w, $h );
/* some one says, this is required for transparency */
imagesavealpha($card, TRUE);
imagealphablending($card, TRUE);
$transparent = imagecolorallocatealpha($card, 0, 0, 0, 127);
imagefilledrectangle($card, 0, 0, imagesx($card), imagesy($card), $transparent);
imagefill( $card, 0, 0, $transparent );
/* temporary border */
//imagerectangle($card, 0,0, imagesx($card)-1, imagesy($card)-1, imagecolorallocate($card, 0, 0, 0) );
/* get total rows of exploded original texts */
$total_rows = count( $txt_process );
/* this will be use for alignment */
$img_width = imagesx( $card );
/* default x axis for the text */
$x = 5 ;
/* set line height, provided height divided by total rows + 2 for bride / groom */
$lh = $h / $total_rows + 2 ;
$lh = $lh < 30 ? 30 : $lh;
$lh-=2;
/* start the loop on each text row */
for( $i = 0; $i < $total_rows; $i++ ) {
$t = $txt_process[$i];
/* define required variables */
$image_txt_font = '';
$image_txt_size = '';
$cur_color = '';
if ( $i == $bride_row || $i == $groom_row ) { /* bride groom row */
$image_txt_font = FONTS_PATH . strtolower( $couple[0] ); /* font-file */
$image_txt_size = $couple[1]; /* font size */
$y = $i == $bride_row ? ($y + $lh/5) : $y + ($lh/10); /* increase y axis by 5 in case of bride row */
$_c = html2rgb( $couple[2] ); /* color code */
$cur_color = imagecolorallocate( $card, $_c[0], $_c[1], $_c[2] );
$_align = $couple[3]; /* alignment */
} elseif ( $i > $bride_row && $i < $groom_row ) { /* and row */
$image_txt_font = FONTS_PATH . strtolower( $main[0] ); /* font-file */
$image_txt_size = $main[1]; /* font-size */
$y = $y - 5; /* decreate the y axis by 15 after bride row and before groom row */
$_c = html2rgb( $main[2] ); /* color code */
$cur_color = imagecolorallocate( $card, $_c[0], $_c[1], $_c[2] );
$_align = $couple[3]; /* alignment */
}
else { /* other rows */
$image_txt_font = FONTS_PATH . strtolower( $main[0] ); /* font-file */
$image_txt_size = $main[1]; /* font-size */
$_c = html2rgb( $main[2] ); /* color code */
$cur_color = imagecolorallocate( $card, $_c[0], $_c[1], $_c[2] );
$_align = $main[3]; /* alignment */
}
/* get the dimensions for the current text line */
$dimensions = imagettfbbox($image_txt_size, 0, $image_txt_font, $t);
$line_Width = abs( $dimensions[4] - $dimensions[0] ); /* this will be used for alignment */
if ( $_align == 'right' ) { /* right align */
/* image with minus line width - 5 for space from border */
$x = ( $img_width - $line_Width ) - 5;
} else if ( $_align == 'center' ) { /* center align */
/* get the ceiling of image width minus line width divided by two */
$x = ceil( ( $img_width - $line_Width ) / 2 );
} else { /* left align */
/* image width minus calculated width plus 5 for space from broder */
$x = ( $img_width - $w ) + 5 ;
}
/* write text to image with font-size, x point, y point, color, font-file, text */
imagettftext($card, $image_txt_size, 0, $x, $y, $cur_color, $image_txt_font, $t);
/* increase the y axis with calculated line height */
$y = $lh + $y;
}
imagecopy($original_card, $card, $positions[0], $positions[1], 0, 0, $w, $h);
//die;
if ( isset( $add_to_cart ) ) {
imagepng( $original_card, CUSTOMER_CARDS . $add_to_cart );
imagedestroy( $original_card );
} else {
header('Content-type: image/png');
imagepng( $original_card );
imagedestroy( $original_card );
}
exit;