Wednesday, September 12, 2012

Screenshots With PHP (GD2)

Found this neat technique to grab a screenshot of a window using PHP, and after some debugging, I was finally able to get it to work.

Here's how:

Note: I believe this only works on windows boxes. If I find a method for a Linux machine, I'll be sure to write something on that as well.

<?php

$browser 
= new COM("InternetExplorer.Application");
$handle $browser->HWND;
$browser->Visible true;
$browser->Navigate("http://www.libgd.org");

/* Still working? */

while ($browser->Busy
{
    
com_message_pump(4000);
}


$im imagegrabwindow($handle0);
$browser->Quit();

imagepng($im"iesnap.png");
imagedestroy($im);

?>

Running just this script alone won't get you the screenshot. You need to go into Windows services and allow whatever web server you are running to interact with the desktop. (If you do not do the below... the images will save as a black filled png file.)

To do this...

Right click 'My Computer' and click Manage.


Find the web server's service (I am using WAMP Server for this demonstration), right click it and hit properties.

Click the 'Log On' tab and check the 'Allow service to interact with desktop' box.

Restart Apache and you should now be able to use the above script. 


Oh, and if you want the image to not have the nasty Internet explorer scroll bars / menus add this to the script:

$browser->Fullscreen = true;

Script credits goes to: http://www.php.net/manual/en/function.imagegrabwindow.php

Thursday, August 23, 2012

WAMP and PHP's cURL Windows 7 x64 bit

I came across, yet another, issue with WAMP server (I'm beginning to question the usability of this application.)

Anyhow; the error I received, even after enabling the php_curl.dll extension, was the following:

'unable to load dynamic library php_curl.dll'

When I checked the Apache error log, I was shown this:


Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.3.13/ext/php_curl.dll' - The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail. in Unknown on line 0.



The fix was to replace the php_curl.dll file in C:\wamp\bin\php\php5.3.13\ext with one of the following:


php_curl-5.3.13-nts-VC9-x64.zip
php_curl-5.3.13-VC9-x64.zip
php_curl-5.4.3-nts-VC9-x64.zip
php_curl-5.4.3-VC9-x64.zip



Credits go to the owner of this blog (Thank you!): http://www.anindya.com/php-5-4-3-and-php-5-3-13-x64-64-bit-for-windows/

Saturday, February 18, 2012

WAMP Server and ImageMagick

Update 9/5/18 - This is more than likely widely outdated. You're probably better off using XAMP over WAMP.

Edit: If you're running x64 bit version of Windows 7 you will need to install wamp, imagick, and php_imagick.dll ALL as x32 bit.

I decided to write this tutorial because this gave me a massive headache for three hours and I want others to not have to go through what I did.

There's millions of guides about this on the internet, but they all seem to be missing one crucial thing.



The .dll file (which we will get to later) MUST be compiled in VC9, or it will not work with php 5.3.5.

My Guide to Install ImageMagick on WAMP Server:


1. Download ImageMagick for Windows here: http://www.imagemagick.org/script/binary-releases.php?ImageMagick=p7k0tc2pra2nbmrgs423c5gsh6#windows (Select the first one on the list ImageMagick-6.7.5-6-Q16-windows-dll.exe)

2. Install to directory C:\imagemagick


and when prompted with a list of selections, choose everything but "Install PerlMagick for ActiveState Perl v51412.2 build 1402" as it causes the installation to error out.


3. Download the VC9 compiled .dll for windows here: http://valokuva.org/builds/

4. Move the downloaded VC9 compiled .dll to this directory: C:\wamp\bin\php\php5.3.5\ext
       *Note: This is the default install directory, it may be different for you.

5. Open up php.ini in a text editor by navigating to (default) C:\wamp\bin\php\php5.3.5\php.ini or by clicking on the WAMP tray icon and selecting php then php.ini.

6. Now that you are in php.ini and editing it, scroll til you find a list of extensions (see picture)

7. After finding the list, add "extension=php_imagick.dll" (without quotes) to the list, as I did above.
     *Note: Do not put a ";" in front of it as it will be commented out.

8. Unfortunately ImageMagick also has issues working with Apache version 2.2  so you need to downgrade to 2.0.63. Don't worry, it's a lot easier than it sounds.

9. To downgrade to Apache 2.0.63 click on the WAMP tray icon, hover over Apache, then Version, then click "Get more..."


9. You will be directed to the WAMP server website, (it may be in French so you can click the English button  in the upper right corner to change it.)

10. Locate and click on "Download Apache Add Ons".

11. In the window that comes up, click on Apache 2.0.63.

12. You will be redirected to a download page, let it download and install it.

13. Start WAMP server again, click on the tray icon, hover over Apache, then click Apache 2.0.63 (Like the image above)

14. Restart all services within WAMP. (Click on Icon, select restart all services).

15. You should now be able to use ImageMagick with no problem.


Hopes this helps anyone that is having issues. Took me a while to figure it out, but thanks to a friend of mine and going through all the steps over and over again, I finally figured out a way to get it working.