Grafika przy pomocy GD
php_7_1.php
|
Najprostszy obrazek. Przestrzeń rysowania wypełniona jednym kolorem.
Pokaż kod PHP
Plik: "php_7_1.php"
1 <?php 2 header("Content-type: image/png");
3 $rys=imagecreate(101,101); 4
5 $colorYellow=imagecolorallocate($rys,255,255,0);
6 imagefill($rys,0,0,$colorYellow);
7 8 imagepng($rys); 9 ?>
Zwiń
|
|
php_7_2.php
|
Kratka.
Pokaż kod PHP
Plik: "php_7_2.php"
1 <?php 2 3 header("Content-type: image/png");
4 $rys=imagecreate(101,101);
5 $colorBlack=imagecolorallocate($rys,0,0,0);
6 $colorYellow=imagecolorallocate($rys,255,255,0);
7 imagefill($rys,0,0,$colorYellow);
8
9 for($i=0;$i<=10;$i++)
10 {
11 imageline($rys,0,10*$i,100,10*$i,$colorBlack);
12 imageline($rys,10*$i,0,10*$i,100,$colorBlack);
13 }
14 15 imagepng($rys); 16 ?>
Zwiń
|
|
php_7_3.php
|
Szachownica.
Pokaż kod PHP
Plik: "php_7_3.php"
1 <?php 2 3 header("Content-type: image/png");
4 $rys=imagecreate(101,101);
5 $colorBlack=imagecolorallocate($rys,0,0,0); 6 $colorRed=imagecolorallocate($rys,255,0,0);
7 $colorYellow=imagecolorallocate($rys,255,255,0);
8 imagefill($rys,0,0,$colorYellow);
9
10 for($i=0;$i<=10;$i++)
11 {
12 imageline($rys,0,10*$i,100,10*$i,$colorBlack);
13 imageline($rys,10*$i,0,10*$i,100,$colorBlack);
14 }
15 16 for($j=0;$j<=10;$j++) 17 { 18 if($j==0 || $j==2 || $j==4 || $j==6 || $j==8) 19 $k=0; 20 else 21 $k=1; 22 23 for($i=$k;$i<=10;$i+=2) 24 { 25 imagefill($rys,10*$i+2,10*$j+2,$colorRed); 26 } 27 } 28 29 imagepng($rys); 30 ?>
Zwiń
|
|
php_7_4.php
|
Kopiowanie obrazków.
Pokaż kod PHP
Plik: "php_7_4.php"
1 <?
2 header("Content-type: image/jpeg");
3 $rys=imagecreate(180,20);
4 $colorBlack=imagecolorallocate($rys,0,0,0);
5 $colorWhite=imagecolorallocate($rys,255,255,255);
6
7 imagefill($rys,0,0,$colorBlack);
8
9 $r0=imagecreatefromjpeg("cyfry/0.jpg");
10 $r1=imagecreatefromjpeg("cyfry/1.jpg");
11 $r2=imagecreatefromjpeg("cyfry/2.jpg");
12 $r3=imagecreatefromjpeg("cyfry/3.jpg");
13 $r4=imagecreatefromjpeg("cyfry/4.jpg");
14 $r5=imagecreatefromjpeg("cyfry/5.jpg");
15 $r6=imagecreatefromjpeg("cyfry/6.jpg");
16 $r7=imagecreatefromjpeg("cyfry/7.jpg");
17 $r8=imagecreatefromjpeg("cyfry/8.jpg");
18 $r9=imagecreatefromjpeg("cyfry/9.jpg");
19 20 //imagecopyresized(dstRys,srcRys,dstX,dstY,srcX,srcY,dstW,dstH,srcW,srcH);
21 imagecopyresized($rys,$r1,0,0,0,0,20,20,20,20);
22 imagecopyresized($rys,$r9,20,0,0,0,20,20,20,20);
23 imagecopyresized($rys,$r7,40,0,0,0,20,20,20,20);
24 imagecopyresized($rys,$r8,60,0,0,0,20,20,20,20);
25
26 imagecopyresized($rys,$r0,90,0,0,0,20,20,20,20);
27 imagecopyresized($rys,$r1,110,0,0,0,20,20,20,20);
28
29 imagecopyresized($rys,$r1,140,0,0,0,20,20,20,20);
30 imagecopyresized($rys,$r2,160,0,0,0,20,20,20,20);
31
32 imagejpeg($rys);
33 ?>
Zwiń
Pliki pomocnicze z cyframi:
0
1
2
3
4
5
6
7
8
9
|
|
php_7_4_2.php
|
Kopiowanie obrazków - automatyczne tworzenie bieżącej daty.
Pokaż kod PHP
Plik: "php_7_4_2.php"
1 <?
2 header("Content-type: image/jpeg");
3 $rys=imagecreate(180,20);
4 $colorBlack=imagecolorallocate($rys,0,0,0);
5 $colorWhite=imagecolorallocate($rys,255,255,255);
6
7 imagefill($rys,0,0,$colorBlack);
8 9 $cyfry = Array(); 10 for($i=0;$i<=9;$i++) 11 $cyfry[$i] = imagecreatefromjpeg("cyfry/".$i.".jpg");
12 13 //pobieram informacje o dacie 14 $rok=date("Y"); 15 $miesiac=date("n");//m - z zerami wiodacymi 16 $dzien=date("j");//d - z zerami wiodacymi 17 18 //buduje rok 19 for($i=0;$i<4;$i++) 20 { 21 $t=substr($rok,$i,1);
22 imagecopyresized($rys,$cyfry[$t],$i*20,0,0,0,20,20,20,20); 23 }
24 25 //a teraz troche inaczej miesiac i dzien 26 $t = (int)($miesiac/10);//wymuszenie aby wynik byl calkowity
27 imagecopyresized($rys,$cyfry[$t],90,0,0,0,20,20,20,20); 28 $t = $miesiac - $t*10;
29 imagecopyresized($rys,$cyfry[$t],110,0,0,0,20,20,20,20);
30
31 $t = (int)($dzien/10);
32 imagecopyresized($rys,$cyfry[$t],140,0,0,0,20,20,20,20); 33 $t = $dzien - $t*10;
34 imagecopyresized($rys,$cyfry[$t],160,0,0,0,20,20,20,20);
35
36 imagejpeg($rys);
37 ?>
Zwiń
Pliki pomocnicze z cyframi:
0
1
2
3
4
5
6
7
8
9
|
|
php_7_5.php
|
Text.
Pokaż kod PHP
Plik: "php_7_5.php"
1 <?
2 header("Content-type: image/png");
3 $rys=imagecreate(180,200);
4 $colorBlack=imagecolorallocate($rys,0,0,0);
5 $colorWhite=imagecolorallocate($rys,255,255,255);
6
7 imagefill($rys,0,0,$colorBlack);
8 9 //imagettftext(rys,size,angle,x,y,color,font,string); 10 $base="/home/fulmanp/public_html/zajecia/internet/zajecia/";
11 imagettftext($rys,20,0,20,50,$colorWhite,$base."tahoma.ttf","Text"); 12 imagettftext($rys,20,30,20,150,$colorWhite,$base."tahoma.ttf","Text"); 13
14 imagepng($rys);
15 ?>
Zwiń
Plik pomocniczy z czcionka:
tahoma.ttf
|
|
php_7_6.php
|
Kalendarz.
Pokaż kod PHP
Plik: "php_7_6.php"
1 <? 2 header("Content-type: image/jpeg"); 3 4 $dayWidth = 100; 5 $dayHeight = 100; 6 $daySeparatorW = 1; 7 $daySeparatorH = 1; 8 9 $imgW = $dayWidth*7 + $daySeparatorW*6; 10 $imgH = $dayWidth*6 + $daySeparatorW*5; 11 12 $baseFont="/home/fulmanp/public_html/zajecia/internet/zajecia/"; 13 14 $rys=imagecreate($imgW,$imgH); 15 16 $colorBlack=imagecolorallocate($rys,0,0,0); 17 $colorWhite=imagecolorallocate($rys,255,255,255); 18 $colorRed=imagecolorallocate($rys,255,0,0); 19 $colorBlue=imagecolorallocate($rys,0,0,255); 20 $colorRedLight=imagecolorallocate($rys,255,180,180); 21 $colorBlueLight=imagecolorallocate($rys,180,180,255); 22 $colorGrayVeryLight=imagecolorallocate($rys,251,251,251); 23 $colorGrayLight=imagecolorallocate($rys,230,230,230); 24 25 imagefill($rys,0,0,$colorGrayVeryLight); 26 27 $color = $colorBlack; 28 $month = 2; 29 $day = 1; 30 $year = 2012; 31 32 /* 33 $data = Array(); 34 for($r=0;$r<6;++$r){ 35 for($c=0;$c<7;++$c){ 36 $w = $c*($dayWidth+$daySeparatorW); 37 $h = $r*($dayHeight+$daySeparatorH); 38 $data[0] = 0 + $w; 39 $data[1] = 0 + $h; 40 $data[2] = $dayWidth - 1 + $w; 41 $data[3] = 0 + $h; 42 $data[4] = $dayWidth - 1 + $w; 43 $data[5] = $dayHeight - 1 + $h; 44 $data[6] = 0 + $w; 45 $data[7] = $dayHeight - 1 + $h; 46 imagepolygon($rys,$data,4,$color); 47 } 48 } 49 */ 50 51 $first = date("N",mktime(0, 0, 0, $month, $day, $year)); 52 $today = date(j); 53 $daysInMonth = 29; 54 $j=0; 55 for($i=-$first+2;$i<=$daysInMonth;++$i,++$j){ 56 if($i>=1){ 57 $c = $j%7; 58 $r = (int)($j/7); 59 60 if($i<$today) 61 if($c==5)//sobota 62 $color = $colorBlueLight; 63 else if($c==6)//niedziela 64 $color = $colorRedLight; 65 else 66 $color = $colorGrayLight; 67 else{ 68 if($c==5)//sobota 69 $color = $colorBlue; 70 else if($c==6)//niedziela 71 $color = $colorRed; 72 else 73 $color = $colorBlack; 74 } 75 76 imagettftext($rys,20,0,($j%7)*($dayWidth+$daySeparatorW)+2,(int)($j/7)*($dayHeight+$daySeparatorH)+23,$color,$baseFont."tahoma.ttf",$i); 77 78 $w = $c*($dayWidth+$daySeparatorW); 79 $h = $r*($dayHeight+$daySeparatorH); 80 $data[0] = 0 + $w; 81 $data[1] = 0 + $h; 82 $data[2] = $dayWidth - 1 + $w; 83 $data[3] = 0 + $h; 84 $data[4] = $dayWidth - 1 + $w; 85 $data[5] = $dayHeight - 1 + $h; 86 $data[6] = 0 + $w; 87 $data[7] = $dayHeight - 1 + $h; 88 imagepolygon($rys,$data,4,$color); 89 } 90 } 91 92 imagepng($rys); 93 ?>c
Zwiń
|
|
|