Portál AbcLinuxu, 10. listopadu 2025 05:39
$rand_str=random_string(5);
//We memorize the md5 sum of the string into a session variable
$_SESSION['image_value'] = md5($rand_str);
//Get each letter in one valiable, we will format all letters different
$letter1=substr($rand_str,0,1);
$letter2=substr($rand_str,1,1);
$letter3=substr($rand_str,2,1);
$letter4=substr($rand_str,3,1);
$letter5=substr($rand_str,4,1);
//Creates an image from a png file. If you want to use gif or jpg images,
//just use the coresponding functions: imagecreatefromjpeg and imagecreatefromgif.
$image=imagecreatefrompng("images/noise2.png");
//Get a random angle for each letter to be rotated with.
$angle1 = rand(-20, 20);
$angle2 = rand(-20, 20);
$angle3 = rand(-20, 20);
$angle4 = rand(-20, 20);
$angle5 = rand(-20, 20);
//Get a random font. (In this examples, the fonts are located in "fonts" directory and named from 1.ttf to 10.ttf)
$font1 = rand(1, 8).".ttf";
$font2 = rand(1, 8).".ttf";
$font3 = rand(1, 8).".ttf";
$font4 = rand(1, 8).".ttf";
$font5 = rand(1, 8).".ttf";
//$font1 = "1.ttf";
//$font2 = "1.ttf";
//$font3 = "1.ttf";
//$font4 = "1.ttf";
//$font5 = "1.ttf";
//Define a table with colors (the values are the RGB components for each color).
//$colors[0]=array(122,229,112);
$colors[0]=array(43,217,25);
//$colors[1]=array(85,178,85);
$colors[1]=array(77,56,56);
$colors[2]=array(213,98,86);
//$colors[3]=array(141,214,210);
$colors[3]=array(68,229,220);
$colors[4]=array(214,141,205);
$colors[5]=array(43,118,241);
//Get a random color for each letter.
$color1=rand(0, 5);
$color2=rand(0, 5);
$color3=rand(0, 5);
$color4=rand(0, 5);
$color5=rand(0, 5);
//Allocate colors for letters.
$textColor1 = imagecolorallocate ($image, $colors[$color1][0],$colors[$color1][1], $colors[$color1][2]);
$textColor2 = imagecolorallocate ($image, $colors[$color2][0],$colors[$color2][1], $colors[$color2][2]);
$textColor3 = imagecolorallocate ($image, $colors[$color3][0],$colors[$color3][1], $colors[$color3][2]);
$textColor4 = imagecolorallocate ($image, $colors[$color4][0],$colors[$color4][1], $colors[$color4][2]);
$textColor5 = imagecolorallocate ($image, $colors[$color5][0],$colors[$color5][1], $colors[$color5][2]);
//Write text to the image using TrueType fonts.
$size = 22;
imagettftext($image, $size, $angle1, 10, $size+15, $textColor1, $font1, $letter1);
imagettftext($image, $size, $angle2, 35, $size+15, $textColor2, $font2, $letter2);
imagettftext($image, $size, $angle3, 60, $size+15, $textColor3, $font3, $letter3);
imagettftext($image, $size, $angle4, 85, $size+15, $textColor4, $font4, $letter4);
imagettftext($image, $size, $angle5, 110, $size+15, $textColor5, $font5, $letter5);
header('Content-type: image/png');
//Output image to browser
imagepng($image);
//Destroys the image
imagedestroy($image);
funkce random_string(5) generuje nahodny rotezec o 5ti znacich
Na vzdalenem serveru se to chova tak, ze se zobrazi pozadi s obrazkem images/noise2.png, ale jednotlive znaky v nem uz se nezobrazi.
Nevim, kde hledat problem
vypis phpinfo ze serveru pro gd:
GD Support enabled
GD Version 2.0 or higher
FreeType Support enabled
FreeType Linkage with freetype
JPG Support enabled
PNG Support enabled
WBMP Support enabled
Nevim jestli neni treba v opravnenich - pro adresar fonts (kde je 8 typu fontu ttf) je nastaveno opravneni cteni a zapis tedy rw-rw-rw-
Dekuji
Asi to nemuze najit ty fonty, zapnete si vypis chybovych hlasek (na zacatek skriptu dejte error_reporting(E_ALL); ini_set('display_errors', true);)
Warning: Could not find/open font in /home/web/www/stranky/random_image.php on line 93 Warning: Could not find/open font in /home/web/www/stranky/random_image.php on line 94 Warning: Could not find/open font in /home/web/www/stranky/random_image.php on line 95 Warning: Could not find/open font in /home/web/www/stranky/random_image.php on line 96 Warning: Could not find/open font in /home/web/www/stranky/random_image.php on line 97 Warning: Cannot add header information - headers already sent by (output started at /home/web/www/stranky/random_image.php:93)Zkusil jsem dat toto
$font1 = "1.ttf"; $font2 = "1.ttf"; $font3 = "1.ttf"; $font4 = "1.ttf"; $font5 = "1.ttf";a soubor 1.ttf se nachazi ve stejnem adresari jako random_image.php. A chova se to stejne, pritom u me na localu to bezi
Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.Skús ku tým fontom dať plnú cestu - /var/www/projects/whatever/fonts/1.ttf, alebo to zadať bez toho .ttf. PS: hneď keď vyriešiš toto, tak by som sa na Tvojom mieste zameral na príkaz for...
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.