Woshiadai Dev Notebook

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. 

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 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.

May 19, 2009

cachegrind for Mac

Filed under: PHP

Start to learn how to do profiling and optimization for PHP code. Found Kcachegrind is handy to visualize XDebug data. There are other similar cachegrind tools:

1. Kcachegrind: you can get it from Macports, check out this if you don’t know how to get started with Macports. Note this might take a long time to install. In my case, I also need to install graphviz to see the call graph.

2. MacCallGrind: this displays the data in a table structure.

3. cachegrindvisualizer: this is a google code project, installed/coded using Adobe AIR, fairly cool UI. If you only need to see the call graph, you probably don’t need Kcachegrind anymore.

4. webgrind: a Web UI for the cachegrind data, also shows a table-like structure.

April 9, 2009

Transparent gif with smooth edge using Imagick

Filed under: PHP, Image Processing

If you have a PNG with transparency and then you want to convert it to a GIF with transparency preserved. Since GIF has only 1-bit transparency, the edge will look jacky. However, if you know a color that will be used as solid background color or an average color for a gradient background, you can still achieve a smooth edge for the transparent gif.

Here is how (assuming the original PNG is circle.png, the edge antialias color is RED):

< ?php
$red = new ImagickPixel('red');
$transparent = new ImagickPixel('transparent');
$im = new Imagick('circle.png');
$im->setImageBorderColor($red);
$im->frameImage($red, 0, 0, 0, 0);
$im->paintOpaqueImage($red, $transparent, 0.0); //replacing RED color with transparent color
$im->setImageFormat(’gif’);
header(’Content-Type: image/gif’);
echo $im;
?>

April 3, 2009

Use Imagick to create images with transparent canvas

Filed under: PHP, Image Processing

update: just found an easier way to create a transparent canvas ;-)
so, you can pretty much ignore all these options listed below now


$im = new Imagick();
$im->newImage(100, 200, new ImagickPixel('transparent')); // use this predefined transparent color string
$im->setImageFormat('png32');
//$im->setImageFormat('gif');
//$im->setImageFormat('jpg'); // note that jpg does not support transparency, so you see a default black background

==== ignore the rest of the post ====

According to ImageMagick Examples canvas page, there are multiple ways to create a transparent canvas.

It is a pity that Imagick PHP library lacks enough documentation, so I had to try my luck with it. For example, I want to create a transparent background, then copy an image with transparency on top of it.


< ?php
try
{
$icon = new Imagick('play.png');

$im = new Imagick();

// put code here to create the transparent canvas, use options below

$im->compositeImage($icon, Imagick::COMPOSITE_COPY, 0, 0);

header(’Content-Type: image/png’);
echo $im;
}
catch (Exception $ex)
{
echo ‘Exception: ‘ . $ex->getMessage() . “\n”;
echo $ex->getTraceAsString();
}
?>

a) setImageOpacity

$im->newImage(200, 200, '#000000', 'png');
$im->setImageOpacity(0.0);

b) paintTransparentImage

$transparent = new ImagickPixel('#000000');
$im->newImage(200, 200, '#000000', 'png');
$im->paintTransparentImage($transparent, 0, 10);

c) matteFloodfillImage

$transparent = new ImagickPixel('#000000');
$im->newImage(200, 200, '#000000', 'png');
$im->setImageBorderColor($transparent);
$im->matteFloodfillImage(0.0, 0.0, $transparent, 200, 200);

d) thresholdImage

$im->newImage(200, 200, '#000000', 'png');
$im->thresholdImage(-1, Imagick::CHANNEL_ALPHA);

e) fxImage

$im->newImage(200, 200, '#000000', 'png');
$im = $im->fxImage('0', Imagick::CHANNEL_ALPHA);

For ImageMagick 6.2.9 12/17/07 Q16, Imagick 2.2.2RC1
a) does not work at all
e) gives the correct result, transparent background for play.png and transparent background for the whole image
others give transparent background for play.png, but black background for the whole image

For ImageMagick 6.4.9-6 2009-03-25 Q16, Imagick 2.2.2
a) b) gives transparent background for play.png and whole image
c) gives an exception
others give white background for play.png, black background for whole image

Images (note that you may want to save the images and open it in a viewer with non-white background to see the difference, especially the last two):

1. Desired ouptput: transparent play button background and transparent canvas:
desired output
2. White play button background and black canvas
white play bg, black canvas
3. Transparent play button background and black canvas
transparent play bg, black canvas






















Get free blog up and running in minutes with Blogsome
Theme designed by Ben de Groot