PHP WinBinder 2: Resource: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by BobS)
 
Line 1: Line 1:
=[http://awuhodynaro.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
{{Nav PHP WinBinder 2}}
{{Nav PHP WinBinder 2}}
'''''WinBinder Part 2 PHC-Win Resources'''''
'''''WinBinder Part 2 PHC-Win Resources'''''
Line 18: Line 17:
It’s worth looking at the mechanics of this process.
It’s worth looking at the mechanics of this process.
{|
{|
|-valign="top"
|-valign="top"
|
|
'''Setup'''
'''Setup'''
Line 25: Line 24:
|
|
'''test.bat'''
'''test.bat'''
<pre>
<pre>
embeder2.exe new test2
embeder2.exe new test2
embeder2.exe add test2 red.bmp ./
embeder2.exe add test2 red.bmp ./
embeder2.exe list test2
embeder2.exe list test2
pause
pause
&lt;/pre&gt;
</pre>
|-valign=&quot;top&quot;
|-valign="top"
|
|
'''Batch file'''
'''Batch file'''
Line 41: Line 40:
'''Run batch file'''
'''Run batch file'''


Run the batch file '''test.bat''' result shown on the right.&lt;br/&gt;It clearly shows the array '''PHP''' and '''md5''' key generated
Run the batch file '''test.bat''' result shown on the right.<br/>It clearly shows the array '''PHP''' and '''md5''' key generated


Note: The md5 is converted to uppercase before being saved to the array.
Note: The md5 is converted to uppercase before being saved to the array.


'''File (resource) access'''&lt;br /&gt;Access a resource using the following code format
'''File (resource) access'''<br />Access a resource using the following code format
{|
{|
|-
|-
|&lt;pre&gt;
|<pre>
res:///PHP/'.strtoupper(md5('./red.exe'))
res:///PHP/'.strtoupper(md5('./red.exe'))
&lt;/pre&gt;
</pre>
|}
|}
|
|
'''Result'''
'''Result'''
&lt;pre&gt;
<pre>


C:\us_wb\phc&gt;embeder2.exe new test2
C:\us_wb\phc>embeder2.exe new test2
'.\test2.exe' created
'.\test2.exe' created


C:\us_wb\phc&gt;embeder2.exe add test2 red.bmp ./
C:\us_wb\phc>embeder2.exe add test2 red.bmp ./
Updated 'res://.\test2.exe/PHP/0374ec857381230fa83049643ce3cdee' with 776 bytes
Updated 'res://.\test2.exe/PHP/0374ec857381230fa83049643ce3cdee' with 776 bytes


C:\us_wb\phc&gt;embeder2.exe list test2
C:\us_wb\phc>embeder2.exe list test2
Res list of '.\test2.exe':
Res list of '.\test2.exe':
PHP
PHP
Line 74: Line 73:
#24
#24


C:\us_wb\phc&gt;pause
C:\us_wb\phc>pause
Press any key to continue . . .
Press any key to continue . . .
&lt;/pre&gt;
</pre>
|}
|}


Line 85: Line 84:
|-
|-
|
|
&lt;pre&gt;
<pre>
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN1),'green.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN1),'green.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR);   
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR);   
wb_set_image($mainwin, &quot;uslogo.ico&quot;);  // Add logo  
wb_set_image($mainwin, "uslogo.ico");  // Add logo  
&lt;/pre&gt;
</pre>
|}
|}
The above functions are expecting an image '''file path'''.
The above functions are expecting an image '''file path'''.
Line 119: Line 118:
If code is running from .exe (EMBEDED true) get resource as a file and save it to disk with same file name.
If code is running from .exe (EMBEDED true) get resource as a file and save it to disk with same file name.
|
|
&lt;pre&gt;
<pre>
// Assign red image to frames
// Assign red image to frames
if(defined('EMBEDED')){    // If main exe Get image save to disk
if(defined('EMBEDED')){    // If main exe Get image save to disk
   file_put_contents('./red.bmp',file_get_contents('res:///PHP/'.strtoupper(md5('./red.bmp'))));
   file_put_contents('./red.bmp',file_get_contents('res:///PHP/'.strtoupper(md5('./red.bmp'))));
}
}
&lt;/pre&gt;
</pre>
|-
|-
|
|
These lines remain unchanged.
These lines remain unchanged.
|
|
&lt;pre&gt;
<pre>
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);  
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);  
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR);  
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR);  
&lt;/pre&gt;
</pre>
|-
|-
|
|
If running from .exe, image no longer required, so delete it.
If running from .exe, image no longer required, so delete it.
|
|
&lt;pre&gt;
<pre>
if(defined('EMBEDED')){    // If main exe
if(defined('EMBEDED')){    // If main exe
   unlink('./red.bmp');    // Delete file  
   unlink('./red.bmp');    // Delete file  
}
}
&lt;/pre&gt;
</pre>
|}
|}
With the exception of image name, each image requires identical code.
With the exception of image name, each image requires identical code.
Line 149: Line 148:
Edit file C:\us_wb\my_app\'''z_basic.phpw'''
Edit file C:\us_wb\my_app\'''z_basic.phpw'''
Replace these lines
Replace these lines
&lt;pre&gt;
<pre>
// Assign images to frames
// Assign images to frames
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);     
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);     
Line 155: Line 154:
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2),  'red.bmp', NOCOLOR);   
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2),  'red.bmp', NOCOLOR);   
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR);  
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR);  
&lt;/pre&gt;
</pre>


With
With
&lt;pre&gt;
<pre>
// Assign red image to frames
// Assign red image to frames
  if(defined('EMBEDED')){    // If main exe Get image save to disk
  if(defined('EMBEDED')){    // If main exe Get image save to disk
Line 178: Line 177:
   unlink('./green.bmp');    // Delete file  
   unlink('./green.bmp');    // Delete file  
  }
  }
&lt;/pre&gt;
</pre>


Replace this line
Replace this line
&lt;pre&gt;
<pre>
   wb_set_image($mainwin, &quot;uslogo.ico&quot;);  // Add logo  
   wb_set_image($mainwin, "uslogo.ico");  // Add logo  
&lt;/pre&gt;
</pre>
With
With
&lt;pre&gt;
<pre>
// Assign logo image to window
// Assign logo image to window
  if(defined('EMBEDED')){    // If main exe Get image save to disk
  if(defined('EMBEDED')){    // If main exe Get image save to disk
   file_put_contents('./uslogo.ico',file_get_contents('res:///PHP/'.strtoupper(md5('./uslogo.ico'))));  
   file_put_contents('./uslogo.ico',file_get_contents('res:///PHP/'.strtoupper(md5('./uslogo.ico'))));  
  }
  }
   wb_set_image($mainwin, &quot;uslogo.ico&quot;);  // Add logo  
   wb_set_image($mainwin, "uslogo.ico");  // Add logo  
  if(defined('EMBEDED')){    // If main exe
  if(defined('EMBEDED')){    // If main exe
   unlink('./uslogo.ico');  // Delete file  
   unlink('./uslogo.ico');  // Delete file  
  }
  }
&lt;/pre&gt;
</pre>
=== Test ===
=== Test ===
* Run z_basic.bat ensure your application runs
* Run z_basic.bat ensure your application runs

Latest revision as of 08:17, 24 November 2010

 

UniServer 5-Nano
PHP WinBinder 2.

WinBinder Part 2 PHC-Win Resources

On the previous page I mentioned image duplication. We included individual images with our distribution package, however PHC-Win adds these to the final exe file.

Without making code changes, these image resources are not directly accessible by WinBinder, hence the reason for adding them to our distribution package.

This page describes code changes required to use images contained within the .exe file, thus eliminating the need for separately including them with the distribution package.

Embeder overview

The embeder creates an executable file where additional files may be added.

These files are added to an array named PHP. Array keys are md5 encoded. A resource file is accessible via its key.

Running this executable creates a unique constant named EMBEDED.

It’s worth looking at the mechanics of this process.

Setup

  • Copy image red.bmp from folder C:\us_wb\my_app and save it to folder C:\us_wb\phc
  • Create a new batch file named test.bat in folder C:\us_wb\phc with the content shown on the right.

test.bat

embeder2.exe new test2
embeder2.exe add test2 red.bmp ./
embeder2.exe list test2
pause

Batch file

  • First line runs the embeder program. It creates a new executable test2.exe
  • Second line adds our image resource red.bmp
  • Last line displays the content of test2.exe

Run batch file

Run the batch file test.bat result shown on the right.
It clearly shows the array PHP and md5 key generated

Note: The md5 is converted to uppercase before being saved to the array.

File (resource) access
Access a resource using the following code format

res:///PHP/'.strtoupper(md5('./red.exe'))

Result


C:\us_wb\phc>embeder2.exe new test2
'.\test2.exe' created

C:\us_wb\phc>embeder2.exe add test2 red.bmp ./
Updated 'res://.\test2.exe/PHP/0374ec857381230fa83049643ce3cdee' with 776 bytes

C:\us_wb\phc>embeder2.exe list test2
Res list of '.\test2.exe':
PHP
        0374EC857381230FA83049643CE3CDEE
        RUN
RT_ICON
        #1
        #2
RT_GROUP_ICON
        #101
#24

C:\us_wb\phc>pause
Press any key to continue . . .

Top

Problem

Our application uses the following code to assign images :

wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR); 
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR); 
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN1),'green.bmp', NOCOLOR); 
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR);  
wb_set_image($mainwin, "uslogo.ico");  // Add logo 

The above functions are expecting an image file path.

This code res:///PHP/'.strtoupper(md5('./red.exe')) returns an image resource, binary content of a file.

The two are incompatible hence WinBinder cannot use an image resource directly.

Solution

The solution is as follows:

  • Write the image resource to a file on disk
  • Allow WinBinder to assign this image file (using its path)
  • Once assigned, it is no longer required, so delete the image file

Top

Code

The code is split into three sections.

  • First section tests if code is running from the .exe using EMBEDED as a reference. If running from .exe, it gets the image resource as a file and saves it to disk with the original file name.
  • Second section contains original lines that remain unchanged.
  • Last section tests if code is running from the .exe and deletes the file from disk if true.

This method allows code to be tested with original images. After compiling, the .exe produced is moved to folder C:\us_wb\my_dis\final for testing.

If code is running from .exe (EMBEDED true) get resource as a file and save it to disk with same file name.

// Assign red image to frames
if(defined('EMBEDED')){    // If main exe Get image save to disk
  file_put_contents('./red.bmp',file_get_contents('res:///PHP/'.strtoupper(md5('./red.bmp'))));
}

These lines remain unchanged.

 wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR); 
 wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR); 

If running from .exe, image no longer required, so delete it.

if(defined('EMBEDED')){    // If main exe
  unlink('./red.bmp');     // Delete file 
}

With the exception of image name, each image requires identical code.

Top

Modifications

Edit file C:\us_wb\my_app\z_basic.phpw Replace these lines

// Assign images to frames
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR);    
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN1),'green.bmp', NOCOLOR); 
wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2),  'red.bmp', NOCOLOR);   
wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2),'green.bmp', NOCOLOR); 

With

// Assign red image to frames
 if(defined('EMBEDED')){    // If main exe Get image save to disk
   file_put_contents('./red.bmp',file_get_contents('res:///PHP/'.strtoupper(md5('./red.bmp')))); 
 }
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED1),'red.bmp', NOCOLOR); 
  wb_set_image(wb_get_control($mainwin, IDC_FRAMERED2), 'red.bmp', NOCOLOR); 
 if(defined('EMBEDED')){    // If main exe
   unlink('./red.bmp');     // Delete file 
 }

// Assign green image to frames
 if(defined('EMBEDED')){    // If main exe Get image save to disk
   file_put_contents('./green.bmp',file_get_contents('res:///PHP/'.strtoupper(md5('./green.bmp')))); 
 }
  wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN1), 'green.bmp', NOCOLOR); 
  wb_set_image(wb_get_control($mainwin, IDC_FRAMEGREEN2), 'green.bmp', NOCOLOR); 
 if(defined('EMBEDED')){    // If main exe
   unlink('./green.bmp');     // Delete file 
 }

Replace this line

  wb_set_image($mainwin, "uslogo.ico");  // Add logo 

With

// Assign logo image to window
 if(defined('EMBEDED')){    // If main exe Get image save to disk
   file_put_contents('./uslogo.ico',file_get_contents('res:///PHP/'.strtoupper(md5('./uslogo.ico')))); 
 }
   wb_set_image($mainwin, "uslogo.ico");  // Add logo 
 if(defined('EMBEDED')){    // If main exe
   unlink('./uslogo.ico');  // Delete file 
 }

Test

  • Run z_basic.bat ensure your application runs
  • Compile
  • Copy z_basic.exe to folder C:\us_wb\my_dis\final
  • Optional: delete image files. (Note: images will be deleted when z_basic.exe is run)
  • Run z_basic.exe

Top

Summary

That completes this tutorial.

To save typing you can find a completed project ready for compiling in folder C:\us_wb\phc\us_example. Copy all files to folder C:\us_wb\my_app, compile and test as above.

Top

Conclusion

  • Designing a Windows application using WinBinder is extremely easy.
  • Compiling a project with PHC-Win really is a snap, and takes just a few mouse clicks.
  • A slight negative for a small Windows application is that the distributed code size is relatively large. This is not a significant issue, with the convenience and ease of use of PHP far outweighs this.
  • That said core elements of WinBinder are being actively worked on. Both Andrew (Frantik) and Alec Gorge “alecgorge” are pushing to reduce core size further; expect significant changes.
  • The WinBinder project remains alive and vibrant. What I really like is that you can produce very powerful Windows applications using these tools. The project is well supported and you will find numerous code examples.
  • If you get stuck with a problem, visit WinBinder’s friendly forum.


Top