imageloadfont

(PHP 4, PHP 5, PHP 7, PHP 8)

imageloadfont β€” Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅Ρ‚ Π½ΠΎΠ²Ρ‹ΠΉ ΡˆΡ€ΠΈΡ„Ρ‚

ОписаниС

function imageloadfont(string $filename): GdFont|false

Ѐункция imageloadfont() Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅Ρ‚ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ Π±ΠΈΡ‚ΠΌΠ°ΠΏ, Π½ΠΎΠ²Ρ‹ΠΉ ΡˆΡ€ΠΈΡ„Ρ‚, ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π΅Π³ΠΎ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

filename

Π€ΠΎΡ€ΠΌΠ°Ρ‚ Ρ„Π°ΠΉΠ»Π° ΡˆΡ€ΠΈΡ„Ρ‚Π° Π΄Π²ΠΎΠΈΡ‡Π½Ρ‹ΠΉ ΠΈ зависит ΠΎΡ‚ Π°Ρ€Ρ…ΠΈΡ‚Π΅ΠΊΡ‚ΡƒΡ€Ρ‹ систСмы. Π­Ρ‚ΠΎ ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ потрСбуСтся ΡΠ³Π΅Π½Π΅Ρ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Ρ„Π°ΠΉΠ» ΡˆΡ€ΠΈΡ„Ρ‚Π° Π½Π° Ρ‚ΠΎΠΌ ΠΆΠ΅ Ρ‚ΠΈΠΏΠ΅ процСссора, Π½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ PHP.

Π€ΠΎΡ€ΠΌΠ°Ρ‚ Ρ„Π°ΠΉΠ»Π° ΡˆΡ€ΠΈΡ„Ρ‚Π°
ΠŸΠΎΠ·ΠΈΡ†ΠΈΡ Π±Π°ΠΉΡ‚Π° Π’ΠΈΠΏ Π΄Π°Π½Π½Ρ‹Ρ… C ОписаниС
Π±Π°ΠΉΡ‚Ρ‹ 0-3 int ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ символов Π² ΡˆΡ€ΠΈΡ„Ρ‚Π΅
Π±Π°ΠΉΡ‚Ρ‹ 4-7 int Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ символа Π² ΡˆΡ€ΠΈΡ„Ρ‚Π΅; часто 32 β€” ΠΏΡ€ΠΎΠ±Π΅Π»
Π±Π°ΠΉΡ‚Ρ‹ 8-11 int Π¨ΠΈΡ€ΠΈΠ½Π° пиксСля ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ символа
Π±Π°ΠΉΡ‚Ρ‹ 12-15 int Высота пиксСля ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ символа
Π±Π°ΠΉΡ‚Ρ‹ 16- char Массив с Π΄Π°Π½Π½Ρ‹ΠΌΠΈ символов, ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡƒ Π±Π°ΠΉΡ‚Ρƒ Π½Π° пиксСль Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ символС, всСго (количСство символов * ΡˆΠΈΡ€ΠΈΠ½Π° * высота) Π±Π°ΠΉΡ‚ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса GdFont ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Бписок измСнСний

ВСрсия ОписаниС
8.1.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса GdFont; Ρ€Π°Π½ΡŒΡˆΠ΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π»ΠΎΡΡŒ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с Ρ‚ΠΈΠΏΠΎΠΌ int.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΡˆΡ€ΠΈΡ„Ρ‚Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imageloadfont()

<?php

// Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π½ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// УстанавливаСм Π±Π΅Π»Ρ‹ΠΉ Ρ„ΠΎΠ½
imagefilledrectangle($im, 0, 0, 49, 19, $white);

// Π—Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ GD-ΡˆΡ€ΠΈΡ„Ρ‚ ΠΈ наносим надпись 'ΠŸΡ€ΠΈΠ²Π΅Ρ‚'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'ΠŸΡ€ΠΈΠ²Π΅Ρ‚', $black);

// Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
header('Content-type: image/png');

imagepng($im);

?>

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagefontwidth() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΡˆΡ€ΠΈΡ„Ρ‚Π°
  • imagefontheight() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ высоты ΡˆΡ€ΠΈΡ„Ρ‚Π°
  • imagestring() - РисуСт строку тСкста Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΡŒΠ½ΠΎ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
6
siker at norwinter dot com ΒΆ
20 years ago
Working under the assumption that the only 'architecture dependant' part of the font files is endianness, I wrote a quick and dirty Python script to convert between the two. It has only been tested on a single font on a single machine so don't bet your life on it working. All it does is swap the byte order of the first four ints.

#!/usr/bin/env python

f = open("myfont.gdf", "rb");
d = open("myconvertedfont.gdf", "wb");

for i in xrange(4):
        b = [f.read(1) for j in xrange(4)];
        b.reverse();
        d.write(''.join(b));

d.write(f.read());

I successfully used this script to convert anonymous.gdf, from one of the font links below, into something useable on Mac OS X.
up
2
alex at bestgames dot ro ΒΆ
20 years ago
Confirmation code generation for preventing automated registrations on a website.

Function arguments are:
$code - the code that you shall random generate
$location - relative location of the image that shall be generated
$fonts_dir - relative location for the GDF fonts directory

This function will create an image with the code given by you and will save it in the directory specified with the name formed by MD5 hash of the code.

You may insert as many font types in the fonts directory as you wish, with random names.

<?php
function generate_image($code, $location, $fonts_dir)
{
     $image  = imagecreate(150, 60);           
     imagecolorallocate($image, rand(0, 100), rand(100, 150), rand(150, 250));
     $fonts = scandir($fonts_dir);
     
     $max = count($fonts) - 2;
     
     $width = 10;
     for ($i = 0; $i <= strlen($code); $i++)
     {     
         $textcolor = imagecolorallocate($image, 255, 255, 255);
         $rand = rand(2, $max);
         $font = imageloadfont($fonts_dir."/".$fonts[$rand]);
         
         $fh = imagefontheight($font);
         $fw = imagefontwidth($font);

         imagechar($image, $font, $width, rand(10, 50 - $fh), $code[$i], $textcolor);     
          $width = $width + $fw;
        
     }
             
     imagejpeg($image, $location."/".md5($code).".jpg", 100);
     imagedestroy($image);       
    
     return $code;
     
}

?>
up
0
matthew at exanimo dot com ΒΆ
20 years ago
Remember - GD fonts aren't antialiased.  If you're planning on using a pre-existing (TrueType) font, you may want to consider using imagettftext() instead of phillip's function.