Recent

Author Topic: [GFX] Translate PHP code to Pascal  (Read 1479 times)

Cobaldblue

  • Newbie
  • Posts: 3
[GFX] Translate PHP code to Pascal
« on: January 26, 2024, 05:53:18 pm »
Hi, I was given the honorable task of transforming a PHP script into a Pascal program.

Unfortunately, I have never worked with graphics functions in Pascal. Therefore I hope that you can help me. I already have some practice developing Windows programs in Pascal / Delphi. However, these were mostly database applications or tools for Windows.  :'(

I'm looking for a way to use the following PHP functions in Pascal.

Here we go:

- Imagecreatetruecolor((($max_x+1)*2048),(($max_y+1)*2048));
//-> Create a new true color image (X,Y)

- Imagecolorallocate($newpicture,255,255,200);
// -> Allocate a color for an image

- ImageCreateFromJPEG
// -> Create a new image from file or URL

- Imagecopy($newpicture,$serverpicture,($x*2048),($y*2048),0,0,2048,2048);
// -> Copy part of an image

- Imagejpeg($newpicture,$savename.'.jpg',100);
// -> Output image to browser or file (save image!)

Thats it!

I've been sitting here at a loss for a while now because I just can't find a solution.

« Last Edit: January 26, 2024, 08:00:24 pm by Cobaldblue »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11982
  • FPC developer.
Re: [GFX] Translate PHP code to Pascal
« Reply #1 on: January 26, 2024, 06:21:01 pm »
Please specify in detail what those functions do?!?!?

Cobaldblue

  • Newbie
  • Posts: 3
Re: [GFX] Translate PHP code to Pascal
« Reply #2 on: January 26, 2024, 08:02:54 pm »
Please specify in detail what those functions do?!?!?

I have expanded my post with a link to the respective PHP command and added a short description of the command below.

I hope this helps...

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [GFX] Translate PHP code to Pascal
« Reply #3 on: January 26, 2024, 08:10:04 pm »
Its better to explain your task than show what other languages do.
Is it correct that you want to load a JPEG image from file and copy part of it as a new JPEG image and save that to file?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [GFX] Translate PHP code to Pascal
« Reply #4 on: January 26, 2024, 09:49:23 pm »
If my above statement was correct, heres a method that does do that, take care, it does not check for anything, it just work.
Code: Pascal  [Select][+][-]
  1. function ExtractJPEG(const AInput, AOutput: string; const AX, AY, AWidth, AHeight: Integer): Boolean;
  2. var
  3.   LSource,
  4.   LDestination: TJPEGImage;
  5.   LRect: TRect;
  6. begin
  7.   Result := False;
  8. // add filecheck here to continue or not
  9.   LSource := TJPEGImage.Create;
  10.   try
  11.     LSource.LoadFromFile(AInput);
  12.     LDestination := TJPEGImage.Create;
  13.     try
  14. // add range checks here if given values are really inside source
  15.       LRect := Rect(AX, AY, AX + AWidth, AY + AHeight);
  16.       LDestination.SetSize(AWidth, AHeight);
  17.       LDestination.Canvas.CopyRect(Rect(0, 0, AWidth, AHeight), LSource.Canvas, LRect);
  18.       LDestination.SaveToFile(AOutput);
  19. // add check here if file was written
  20.       Result := True;
  21.     finally
  22.       LDestination.Free;
  23.     end;
  24.   finally
  25.     LSource.Free;
  26.   end;
  27. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

domasz

  • Hero Member
  • *****
  • Posts: 554
Re: [GFX] Translate PHP code to Pascal
« Reply #5 on: January 27, 2024, 11:53:25 am »
Code: Pascal  [Select][+][-]
  1. function Imagecreatetruecolor(Width, Height: Integer): TBitmap;
  2. begin
  3.   Result := TBitmap.Create;
  4.   Result.SetSize(Width, Height);
  5. end;
  6.  
  7. function ImageColorAllocate(Bmp: TBitmap; R,G,B: Byte): Cardinal;
  8. begin
  9.   Result := (B shl 16) + (G shl 8) + R;
  10. end;
  11.  
  12. function ImageCreateFromJPEG(Filename: String): TBitmap;
  13. var jpg: TJpegImage;
  14. begin
  15.    jpg := TJpegImage.Create;
  16.    jpg.LoadFromFile(Filename);
  17.  
  18.    Result := TBitmap.Create;
  19.    Result.Assign(jpg);
  20.  
  21.    jpg.free;
  22. end;
  23.  
  24. function Imagejpeg(Bmp: TBitmap; Filename: String; Compression: Byte): String;
  25. var jpg: TJpegImage;
  26.       Mem: TMemoryStream;
  27. begin
  28.   Result := '';
  29.   jpg := TJPEGImage.Create;
  30.   try
  31.     Jpg.Assign(Bmp);
  32.     Jpg.CompressionQuality := Compression;
  33.     Jpg.ProgressiveEncoding := False;
  34.     Jpg.Compress;
  35.  
  36.     if Filename = '' then begin
  37.       Mem := TMemoryStream.Create;
  38.       jpg.SavetoStream(Mem);
  39.       Mem.Position := 0;
  40.       SetLength(Result, Mem.Size);
  41.       Mem.Read(Result[1], Mem.Size);
  42.       Mem.Free;
  43.    end
  44.    else
  45.     Jpg.SaveToFile(FileName);
  46.   finally
  47.     Jpg.Free;
  48.   end;
  49. end;
  50.  
  51.  
  52. function ImageCopy(Dst, Src: TBitmap; DstX, DstY, SrcX, SrcY, SrcWid, SrcHei: Integer): Boolean;
  53. begin
  54.   Result := True;
  55.   Dst.Canvas.CopyRect(Rect(DstX, DstY, DstX+SrcWid, DstY+SrcHei), Src.Canvas, Rect(SrcX, SrcY, SrcX+SrcWid, SrcY+SrcHei));
  56. end;  
  57.  
« Last Edit: January 27, 2024, 12:15:13 pm by domasz »

Cobaldblue

  • Newbie
  • Posts: 3
Re: [GFX] Translate PHP code to Pascal
« Reply #6 on: January 29, 2024, 05:18:12 pm »
Hello!

I have a lot to do this week. However, as soon as I find time I will consider the proposed solutions.

But the solution to my problems seems to be within reach. Thank you for YOUR time and effort.

However, I would like to briefly address the question of what the program should do. We have a program with a graphical display and the same program running on multiple servers.
A second program takes screenshots of the respective screen at certain intervals and emails them to another server.
Here the 6 or 8 screenshots were copied together into 4 or 2x 4 images on a web server and the image was then displayed on a large screen monitor.

The web server should now be switched off for security concerns and the program should simply be run on the server. This saves you a step.

Not really a big project.

 

TinyPortal © 2005-2018