unit Unit1;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, httpdefs, fpHTTP, fpWeb,
Graphics,
fpimage, FPCanvas, FPImgCanv,
FPWriteJPEG,
FTFont;
type
{ TFPWebModule1 }
TFPWebModule1 = class(TFPWebModule)
procedure imageRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
private
public
end;
var
FPWebModule1: TFPWebModule1;
implementation
{$R *.lfm}
{ TFPWebModule1 }
procedure TFPWebModule1.imageRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
var canvas : TFPcustomCanvas;
image : TFPCustomImage;
writer : TFPCustomImageWriter;
MyStream : TMemoryStream;
img_src : string;
AFont: TFreeTypeFont;
begin
img_src := ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'img.jpg';
if FileExists(img_src) then
begin
AResponse.ContentType := 'image/jpeg';
FTFont.InitEngine;
FontMgr.SearchPath := ExtractFilePath(ParamStr(0));
AFont := TFreeTypeFont.Create;
image := TFPMemoryImage.Create (0,0);
image.LoadFromFile(img_src);
Canvas := TFPImageCanvas.Create (image);
Writer := TFPWriterJPEG.Create;
Canvas.Font:=AFont;
Canvas.Font.Name:='Arial';
Canvas.Brush.FPColor := TColorToFPColor(clWhite);
Canvas.Font.FPColor := TColorToFPColor(clYellow);
Canvas.Font.Size := 18;
Canvas.TextOut(20, 40,'Hello world'); //Comment this line to run correctly
MyStream := TMemoryStream.Create;
image.SaveToStream(MyStream, writer);
AResponse.ContentStream := MyStream;
Canvas.Free;
image.Free;
writer.Free;
AFont.Free;
end else begin
AResponse.ContentType := 'text/html';
AResponse.Contents.Text:='Image not found : ' + img_src;
end;
Handled := true;
end;
initialization
RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
end.