Woshiadai Dev Notebook

October 30, 2010

moving to mwebhack.blogspot.com

Filed under: Uncategorized

Finally, I got tired of the blogsome editor (both not-so-rich WYSIWYG editor and HTML editor) and decided to migrate to Blogger:

http://mwebhack.blogspot.com

It actually took a while to figure out how to export all existing blogsome posts and import into Blogger. If you are interested, you can take a look at my post there.

See you there in the new URL ;-)  

October 26, 2010

failed to run code reformat in TextMate

All of a sudden, I cannot format/reformat the code in Textmate. Got this error:

PHP Warning: require(/lib/beautify.php): failed to open stream: No such file or directory in /private/tmp/temp_textmate.4tD2ED on line 19

Searched around the Web and it seems to be caused by php not setting $_ENV property due to the variables_order not properly set in /etc/php.ini (assuming you are using php shipped with Mac, if you are using php from Macports, do it accordingly).

The fix is to add/update the variables_order value to something like variables_order = "EGPCS"

References:

 

  1. Fixing Textmate’s PHP Commands with MacPorts PHP
  2. PHP core directives for php.ini explaining variables_order 

 

July 9, 2010

Creating shadows with ImageMagick

Filed under: PHP, Image Processing

1. Shadows for an image (create a shadow using Imagick::shadowImage and overlay the original image on top)

<?php
/* Read the image into the object */
$im = new Imagick( ‘totoro.png’ );
$im->setImageFormat("png");

/* Make the image a little smaller, maintain aspect ratio */
$im->thumbnailImage( 200, null );

/* Clone the current object */
$shadow = $im->clone();

/* Set image background color to black (the color of the shadow) */
$shadow->setImageBackgroundColor( new ImagickPixel( ‘black’ ) );

/* Create the shadow */
$shadow->shadowImage( 80, 3, 5, 5 );

/* Imagick::shadowImage only creates the shadow. That is why the original image is composited over it */
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
$content = $shadow->getImageBlob();

$shadow->destroy();
$im->destroy(); 

/* Display the image */
header( "Content-Type: image/jpeg" );
header(’Content-Length: ‘ . strlen($content)); 
echo $content;
?>

2. Shadow for text

Note that the trick for multi-line text is to insert a new line character (\n) in your string. You can also use the rotation degree to control the layout, but make sure you use the correct size text canvas image because when you queryFontMetrics, it is based on the horizontal layout.

<?php
// overlay the text
$draw = new ImagickDraw();
$draw->setFont(’Helvetica’);
$draw->setFontSize(13);
$draw->setFillColor(new ImagickPixel(’#B8B8B8′));

$text = "HELLO\nWORLD!"; // for multi-line text, insert new line char

$textcanvas = new Imagick();
$textprops = $textcanvas->queryFontMetrics($draw, $text, true); //you need to turn multiline on, the last param
$text_w = intval($textprops[’textWidth’]);
$text_h = intval($textprops[’textHeight’]);
$textcanvas->newImage($text_w+20, $text_h+20, new ImagickPixel(’transparent’)); //give 20 pixel spaces around the text
$textcanvas->setImageFormat(’png’);
$textcanvas->annotateImage($draw, 10, $text_h, 0, $text);

// create shadow
$shadow = $textcanvas->clone();
$shadow->setImageBackgroundColor(new ImagickPixel(’black’));
$shadow->shadowImage(80, 3, 5, 5);
$shadow->compositeImage($textcanvas, Imagick::COMPOSITE_OVER, 0, 0);
$shadow_w = $shadow->getImageWidth();
$shadow_h = $shadow->getImageHeight();

// overlay the shadowed text on the given background image
$bg = new Imagick(’background.png’);
$bg->setImageFormat(’png’);
$bg_w = $bg->getImageWidth();
$bg_h = $bg->getImageHeight();

//$bg->compositeImage($shadow, Imagick::COMPOSITE_OVER, 0, ($bg_h-$shadow_h));
$bg->compositeImage($shadow, Imagick::COMPOSITE_OVER, 0, 0);

// let’s show the image
$content = $bg->getImageBlob();
$textcanvas->destroy();
$shadow->destroy();
$bg->destroy();

header(’Content-Type: image/png’);
header(’Content-Length: ‘ . strlen($content));
echo $content;
?> 


 

Somehow, I still don’t know how to adjust the line spacing between multi-lines. I also found that ImagickDraw::setFontWeight did not work for me. Anyone knows how to fix those two issues? 

 

Reference:

1. Mikko’s blog (the best place for ImageMagick samples, who can beat the IM developer himself) 

June 23, 2010

LuaEclipse setup on Snow Leopard

Filed under: Lua

So, the instructions on the LuaEclipse main site won’t work for the latest Eclipse and I had many errors last time. Today, I finally got LuaEclipse 1.3.1 set up and running now. Here are the steps:

 

  1. install lua and luarocks from MacPorts (sudo port install lua luarocks)
  2. install LuaProfiler using luarocks (luarocks install luaprofiler) 
  3. download and install Eclipse 3.4.2, this version works with current LuaEclipse release
  4. install LuaEclipse 1.3.1 from the update site, note that the official LuaEclipse installation guide only shows update site for 1.0 and 1.2. You need to use this update site: http://luaeclipse.luaforge.net/preview/update-site/macosx.carbon/, for other OS, you can use the update sites found from here
  5. configure LuaEclipse through Eclipse Preferences > Lua, add Lua Interpreter (point it to /opt/local/bin/lua) and Lua Profiler (point it to /opt/local/var/luarocks/lib/5.1/profiler.so)
  6. all set, you can create a new Lua project and start writing Lua and debugging, profiling it…
Interestingly, LuaEclipse developers have started working on LuaEclipse 2. Here is the installation instruction and the main site on GitHub.

 

June 19, 2010

setting up Lua on Mac

Filed under: Lua

Recently, fell insterested in Lua due to some work with lighttpd. So, want to share some experience setting up an IDE-ish environment (of course, VIM is always the best editor).

1. install lua and luarocks through MacPorts: http://www.macports.org/ports.php?by=name&substr=lua

2. failed attempt setting up LuaEclipse following instructions at http://luaeclipse.luaforge.net/manual.html#luadoc

Update: Finally I got LuaEclipse set up correctly, please see my latest post for instructions.

LuaEclipse is a great IDE, however developers did not seem to be updating the project often and it does not work due to some 64bit jni library issues on Snow Leopard.

I first tried to install through update manager from Aptana Studio 2.0, got this error:


/Applications/Aptana Studio 2.0/configuration/org.eclipse.osgi/bundles/224/1/.cp/libluajava-1.1.jnilib: no suitable image found. Did find: /Applications/Aptana Studio 2.0/configuration/org.eclipse.osgi/bundles/224/1/.cp/libluajava-1.1.jnilib: mach-o, but wrong architecture

Could not initialize class org.keplerproject.luajava.LuaState

Then I tried to install a 64bit Eclipse and install the LuaEclipse through update site and manually install the plugin zip, both failed

a) through the update site, it does not work, always complaining one dependent package is missing from update site

b) download the plugin and install manually, installed fine, but cannot run, got error a specific directory is missing

Conclusion: forget about LuaEclipse

3. Install a TextMate bundle: http://netcetera.org/cgi-bin/tmbundles.cgi?bundle=%4C%75%61

TextMate is the choice of text editor for Mac. This Lua bundle worked perfectly for me. The only thing is that TextMate might be installed differently, so you may need to tweak the script to the "Library/Application Support/TextMate/Bundles" in your home directory, at least I had to do that. Also note that you need to configure the PATH variable in TextMate so that the lua executable can be found by TextMate.

If you are interested in Lua, Programming in Lua (2nd) is the best book to cover the basics from the original Lua developer.

November 11, 2009

set up PHP and Apache on Snow Leopard

Filed under: PHP, Mac

It took me quite some trouble to get imagick working on the snow leopard. 

First, I tried to install xampp and mamp, which are two easy ways to get PHP, apache, mysql up and running. I used to have no issue with xampp, but the snow leopard has problems with locally complied imagick.so in xampp. So, I tried mamp as well, imagick simply does not work and kept getting me "cannot find Imagick class" error.

So, I had to try to use the PHP and apache that ships with snow leopard. I simply followed the instructions in this blog post and it worked out well for the most part.

The only problem I am having is with the userdir apache module. Using the userdir module, you can just drop web applications to /Users/<username>/Sites. But no matter how hard I tried, I just could not get this working and had to give up.

UPDATE: found this post saying the permission denied problem is related to the user permission of _www. Apache runs as _www under _www group that need to have access to /Users/<username>/Sites. I’ve added xr to everyone on that directory, not sure why it is still not working. If you want to give it a try you can do it. 

October 31, 2009

upgrading to snow leopard

Filed under: Apple, Mac

Finally, I decided to do the upgrade. It was not as smooth as it looks, especially that I turned on 64 bit as default. Normally, SL runs in 32 bit mode, you need to hold 6 and 4 keys during the boot process to get into 64 bit mode. But you can also change the config to run it as default, instruction is here.

Most of the applications still runs fine, here is a few exceptions that I encountered and luckily all of them have solutions so far. Here is a list of compatibility check for SL, note that it is not the default 64 bit mode. 

1. menumeter: This is a small monitoring tool that I use every day, to check network, CPU, memory usage. Good thing is it displays info on the menu bar. Old installation not working anymore, just get the latest and reinstall.

2. delicioussafari: This is a plugin for Safari that saves and opens your delicious online bookmarks. To fix the issue, just get the latest and reinstall. The new version even has a new BMW-like button ;-)  

3. macfuse: This is a handy tool to help mount various file systems. Here is the fix for 32 bit SL mode, however it did not work for 64 bit mode. You can use this pre-compiled dmg for 64 bit mode. It runs great for me. You can also roll your own following the instructions here.

4. TextMate: It has lots of issues with SL, especially due to the upgrade of Ruby. First, try to get the latest release possible. But still, I am having issues with ⌘R. Textmate blog has some workarounds and I have not tried them yet, hopefully they will fix the problems in the next release.

5. MacPorts: This is another thing that caused many headache until I found this nice blog that suggests a fresh install. It worked all fine. However, occasionally, some ports will still report error of unmatched architecture. This blog also covers "Upgrading to Snow Leopard Part 1 to 7", talking about python, mysql and other upgrade tips, very handy! He also talks about building 32 bit packages using arch and lipo.

6. VMWare Fusion: The old 2.x version even won’t start in 64 bit and asks you to restart SL in 32 bit. So, you need to get the latest Fusion 3.0. The upgrade is not free.

After a week of being little white mouse running 64 bit SL. I’d say maybe running 64 bit does cause more issues than benefits. But we can debate over it. Here is a good intro if you are interested.

The best part for me is that I got back 12G free space, very interesting. It seems to be a combination of using 1000 for 1024 in calculation and cut the new OS installation size.

 

June 26, 2009

Subversion and SSH setup in Netbeans 6.7 RC3 on Mac OS X

Filed under: Java, PHP, Apple, Mac

Netbeans has been making a lot of progress since version 6 and the latest 6.7 RC3 is even better. For those Eclipse fans, I strongly suggest that you check out Netbeans. It feels much faster and consumes less memory. The plugins are more organized and easy to manage (I think the plugin management in Eclipse is really messy).

The PHP plugin for Netbeans is out of beta and it is quite user friendly and versatile. So, I decided to switch from PDT to Netbeans for my PHP development.

Netbeans comes with Subversion support. However, it is not so smooth to set up Subversion over SSH on Mac OS X due to the ssh-askpass problem. This is a Mac OS X issue, not a Netbeans issue.

  1. Download and install Netbeans 6.7 RC3 (as of June 25, 2009).
  2. The subversion that comes with Mac OS X (/usr/bin/svn, 1.4.4 (r25188)) is quite outdated and it has problems working with newer versions of the SVN server, probably due to different format of the metadata. You need to use MacPorts to install a more recent subversion (if you don’t know how to install MacPorts, check out this). Just type this in your terminal: port install subversion, it might take a white. The subversion from MacPorts is installed in /opt/local/bin, so make sure you put /opt/local/bin before /usr/bin in your PATH environment variable.
  3. Add SVNROOT and SVN_SSH environment variables to your .bashrc: export SVNROOT=svn+ssh://your_svn_server/ export export SVN_SSH=/usr/bin/ssh
  4. Launch Netbeans, go to preferences->Miscellaneous->Versioning->Subversion, for “path to SVN executable file”, enter /opt/local/bin
  5. Now, it seems like everything is ready. However, when you try to do update/diff and other operations, you get error: ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory. I did some Yahoo! search and the problem is that Netbeans wants to communicate with SVN server over ssh without asking your password using ssh-askpass, but it is not available. This is a general ssh issue on Mac OS X. I found two solutions: solution 1 and solution 2. I only tried solution 2 and it worked perfectly for me. Here are the simple steps: a) download script from here (note that I had to add .jpg since blogsome only supports image upload) and save it in /usr/libexec. Change ssh-askpass script to executable: wget wget http://woshiadai.blogsome.com/images/sshaskpass.jpg -O ssh-askpass sudo cp ssh-askpass /usr/libexec/ssh-askpass sudo chmod a+x /usr/libexec/ssh-askpass b) add two more environment variables: export SSH_ASKPASS=macos-askpass export DISPLAY=:0
  6. Finally, you can use subversion actions from inside Netbeans. You will be asked for password for the first time, then it will be remembered for future activities. Enjoy the Netbeans! ;-)

June 19, 2009

Screen Sharing

Filed under: Apple, Mac

I have been using Mac for quite a while, but did not have much time to explore many of its features. Today, I managed to find how to do easy VNC to my linux box using Screen Share application, a hidden treasure.

First, follow the instructions here to set up the Screen Share application. Somehow I could get the advanced toolbar working.

Second, you need to set up the VNC server on your linux box. In my case, Redhat Enterprise comes with VNC server installed already. All I need is to configure it properly. Follow this nicely written tutorial to start your VNC server. There are two ways in this tutorial: use the build-in remote desktop setup, or the DIY configuration. I chose the 2nd option since it is much flexible and the first one did not really worked for me.

Third, connect to your VNC server. In my case, I just typed in hostname:5091 and clicked connect in Screen Sharing, then it starts working, really cool!

I think you should be able to do the same with a Windows machine running VNC server, but I did not try that.

June 18, 2009

ICO image processing with Imagick

Filed under: PHP, Image Processing

Used to have an issue with Imagick when I convert ICO image into png. Somehow when I create the Imagick object from image content string, the final result is a crappy image. But when I create the Imagick object directly from a file, it works fine, with only the problem of a different size.

So, I filed a bug (http://pecl.php.net/bugs/bug.php?id=15701), but still got no resolution.

Got some time recently and did a bit research, found that the problem is with the ICO format: it may contain the same image with different sizes. So, when you convert the format directly, it will not work. It is similar in the case when you try to convert an animated GIF image into JPG or PNG, you get one of the frames instead of an animated image.

There are two solutions:

  1. use foreach($ico as $im) loop to get one image and just convert that image
  2. use flattenImages function before the format conversion

Note that after the format conversion, the image dimensions might be different from when you directly open the ICO image from a browser or image viewer. You probably need to resize the image manually.

The bug report has some code snippets if you want to try it out.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham