-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_2create3GiftsPerPage.php
188 lines (144 loc) · 4.13 KB
/
model_2create3GiftsPerPage.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
<?php
ini_set('max_execution_time', 300);
/*
To create the gift cards the first thing we need to do is create a image with this gift card
and after that we create a pdf file with this image inside.
I've tried to create the pdf with the text embeded but it did not work because the font could must to be outlined.
More info: http://us.moo.com/help/faq/using-my-own-artwork.html
*/
// Txt file with the codes
$codes = file( 'codes.txt' );
// Fonts that will be used
$fontOmnesBold = 'assets/fonts/OmnesBold.otf';
$count = 1;
// Fist create all the images
foreach ( $codes as $code ){
// First lets create the image
// $image = imagecreatefrompng( 'assets/model_2/giftcard_back.png' );
$image = imagecreatefrompng( 'assets/model_2/Crunchbutton_GiftCard_back_big.png' );
// Antialiases
imagealphablending( $image, true );
imageantialias( $image, true );
// Set the colors
$white = imagecolorallocate( $image, 255, 255, 255 );
// Put the texts
imagettftext( $image, 67, 0, 890, 570, $white, $fontOmnesBold, 'crunchbutton.com/giftcard/' . $code );
imagesetthickness ( $image, 5 );
// Path where the image wil be saved
$imgsrc = 'temp/' . $count . '.png';
// header('Content-Type: image/png');
// imagepng( $image );
// exit;
imagepng( $image, $imgsrc );
imagecolordeallocate( $image, $white );
// Destroy the image
imagedestroy( $image );
$count++;
}
$count--;
//Second create the pdf
// PDF Library
require('lib/fpdf.php');
// Page size in millimetter - (Letter)
$pageWidth = 215;
$pageHeight = 279;
$giftCardWidth = 215;
$giftCardHeight = 90;
$collumns = 1;
$rows = 4;
$marginPageTop = 0;
$marginPageLeft = 0;
$marginTop = 0;
$marginLeft = 0;
$giftCards = [];
// Order in stacks
for ($i = 0; $i < $count; $i++) {
$giftCards[ $i ] = 'temp/' . ( $i + 1 ) . '.png';
}
$slotsPerPage = ( $collumns * $rows );
$numberOfPages = ceil( $count / $slotsPerPage );
$totalGifts = $count;
$giftsPerPosition = ceil( $totalGifts / $numberOfPages );
$left = $totalGifts % $numberOfPages;
$perPosition = array();
$giftCardsOrdered = array();
if( $left != 0 ){
for( $i = 0; $i < $numberOfPages; $i++ ){
if( $left > 0 ){
$perPosition[ $i ] = $giftsPerPosition;
$left--;
} else {
$perPosition[ $i ] = $giftsPerPosition - 1;
}
}
} else {
for( $i = 0; $i <= $numberOfPages; $i++ ){
$perPosition[ $i ] = $giftsPerPosition ;
}
}
$startsAt = array();
$sum = 0;
for( $i = 0; $i < sizeof( $perPosition ); $i ++ ){
$startsAt[ $i ] = $sum;
$sum = $sum + $perPosition[ $i ];
}
for( $i = 0; $i < $giftsPerPosition; $i++ ){
for( $j = 1; $j <= $numberOfPages; $j++ ){
if( sizeof( $giftCardsOrdered ) < sizeof( $giftCards ) ){
$index = $startsAt[ $j - 1 ] + $i;
$giftCardsOrdered[] = $giftCards[ $index ];
}
}
}
// Create the pdf
$pdf = new FPDF( 'P', 'mm', array( $pageWidth, $pageHeight ) );
$pdf->AddPage();
$row = 1;
$collumn = 1;
$page = 1;
$giftCardsOnThisPage = 0;
$giftCardsOnThisRow = 0;
// Draw vertical the cut lines
/*
$pdf->SetLineWidth( 0.01 );
$pdf->SetDrawColor( 175, 175, 175 );
for( $j = 0; $j <= $collumns; $j++ ){
$l = $marginPageLeft + ( $j * $giftCardWidth );
$pdf->Line( $l, 0, $l, $pageHeight);
}
for( $j = 0; $j <= $rows; $j++ ){
$t = $marginPageTop + ( $j * $giftCardHeight );
$pdf->Line( 0, $t, $pageWidth, $t );
}
*/
for ( $i = 0; $i < $count; $i++ ) {
$imgsrc = $giftCardsOrdered[ $i ];
$positionY = ( ( ( $row - 1 ) * ( $giftCardHeight + $marginTop ) ) + $marginPageTop );
$positionX = ( ( ( $collumn - 1 ) * ( $giftCardWidth + $marginLeft ) ) + $marginPageLeft );
$pdf->Image( $imgsrc, $positionX, $positionY, -300, 'PNG' );
$collumn = ( $collumn == 1 ) ? 2 : 1;
if( $collumn == 1 ){
$row++;
}
$giftCardsOnThisPage++;
if( $giftCardsOnThisPage == ( $collumns * $rows ) ){
$pdf->AddPage();
/*
for( $j = 0; $j <= $collumns; $j++ ){
$l = $marginPageLeft + ( $j * $giftCardWidth );
$pdf->Line( $l, 0, $l, $pageHeight);
}
for( $j = 0; $j <= $rows; $j++ ){
$t = $marginPageTop + ( $j * $giftCardHeight );
$pdf->Line( 0, $t, $pageWidth, $t );
}
*/
$giftCardsOnThisPage = 0;
$row = 1;
$collumn = 1;
}
}
// $pdf->Output( 'pdfs/GiftCards.pdf' );
$pdf->Output();
// echo 'done!';
?>