Archive for August, 2011

Logic questions 1

Monday, August 29th, 2011

A couple of days ago I received a call from a south american country.

They are searching for web developers to work with them in some projects involving Google. They read my profile on Linkedin and decided to contact me. It was a very interesting interview due to the fact that it was focused both in evaluating my technical and logic skills.

One of the questions that were made was quite simple but I liked it:

Given a random integer ¿how would you code a function that returns the mirrored integer?

It might sound simple, but I’ve been working with PHP for years and it’s not a strong typed language so my first answer was to deal with it as a string:

<?php
function mirror($number){
    return strrev($number);
}
?>

But then the interviewer told me that I should’t treat data as a string but as a number… ok… so I started thinking and came up with this:

<?php
function mirror0($number){
    $ret = 0;
    $prev = 0;
    $go = true;
    do{
        $quotient = (int) floor($number/10);
        $rest = $number - ($quotient * 10);
        $ret = ($prev * 10) + $rest;
        $prev = $ret;
        if($quotient===0){
            $go = false;
        }
        $number = $quotient;
    }while($go);
    return $ret;
}
?>

He liked my answer but told me that perhaps I could do it simpler by using a logarithm… this stuck into my mind so after the call ended I wrote a piece of code:

<?php
function mirror1($number){
    $ret = 0;
    $quotient = floor($number/10);
    $rest = $number - ($quotient * 10);
    if($quotient == 0){
        $ret = $rest;
    }
    else{
        $ret = ($rest * (pow(10,floor(log($number,10))))) + mirror1($quotient);
    }
    return $ret;
}
?>

I wrote a small benchmark code and the difference is unnoticeable.

Anyway, I’m just posting this 3 even though there might be a lot of different ways.

Zend Framework – View – $this->url()

Thursday, August 11th, 2011

Been using ZF a lot. It hasn’t amazed me because even though it’s really powerful it’s still very complicated to achieve simple task and the official documentation is very very messy.

Anyway, I have this layout for a project with a main menu.

Each of the menu options link to a different controller, instead of hard coding the menu links I used ZF url helper so for every link I was using something like this:

$this->url(array('controller'=>'console','action'=>'index'));

Which when ran gave me the following link

/[project_folder]/public/console

This looked just fine. Later on I created a product’s catalogue and from there a link to see a product’s detail. The link was:

$this->url(array('controller'=>'catalogue','action'=>'details','pid'=>[product_id]));

Everything worked just fine so I clicked the products details link. Everything looked fine. Then I clicked one of the main menu links to get to another section of the web page and then a bug popped.

For some reason all the links where appended with the GET parameters so when I clicked on the info link it took me to

/[project_folder]/public/info/pid/[product_id]

And obviously I was getting an error because there was no ‘pid’ action for the ‘info’ controller.

I searched and read a couple of google results, give the documentation a try (it sucks) and after a while I ended up in a simple blog entry with the solution.

I only needed to change my code to this:

$this->url(array('controller'=>'console','action'=>'index'),null,true);

Now GET params are not appended to the URL’s and everything works fine 😀

Imagick – Postscript delegate failed

Wednesday, August 10th, 2011

This new project I’m working on requires me to extract an image from a PDF file and show a preview on the web page. I knew this was possible but I didn’t knew how.

After googling it for a while I ended with some interesting results that pointed me to ImageMagick. It’s a nice tool to transform/extract images from one format to another. It is supported by many languages as C, Perl, Python and PHP. Since I am coding with PHP I went for it and activated the imagick extension for PHP on my Zend Server CE.

I’m using a Mac with Lion OS X.

I found a really good blog that has a whole section for Imagick with pretty neat results here.

After reading for a while I was ready to do my first test, so I used a code similar to this one:

<?php
/* Read page 1 */
$im = new imagick( 'test.pdf[0]' );
/* Convert to png */
$im->setImageFormat( "png" );
/* Send out */
header( "Content-Type: image/png" );
echo $im;
?>

And then ran it on my browser but to my surprise it was throwing an exception:

Postscript delegate failed `test.pdf': No such file or directory

The path was fine, I tested it with a jpg and a png image and it worked so I was really confused and started to dig into the issue. After a while I ended up reading ImageMagick documentation and somewhere I read that it uses third party commands to accomplish format transformations from vector images (like the ones stored within a pdf). Lastly I went to the server error logs and found this:

sh: gs: command not found

I guessed from previous experience that this message meant the following:

sh -> execute a shell command
gs -> ghostscript command
[text] -> error message

So it meant that the server was not being able to execute the ghostscript command, ¿really?
I went to a console and typed in:

which gs

It returned the command path so I knew I had ghostscript installed so the problem should be that the server user didn’t had the path set properly.
To check if my assumption was correct I typed the following in the console:

echo $PATH

And I got the PATH env variable.
On my PHP script I added the following line:

echo getenv('PATH');

And my script displays the PATH env variable being used by my web server. The result was different and the path where the ghostscript executable was not being included.
Now I was able to solve the issue and it was really simple. It required for me to provide the correct path for my server.
Now my script look like this:

<?php
putenv('PATH=' . getenv() . ':[absolute_path_where_the_ghostscript_executable_is]');
/* Read page 1 */
$im = new imagick( 'test.pdf[0]' );
/* Convert to png */
$im->setImageFormat( "png" );
/* Send out */
header( "Content-Type: image/png" );
echo $im;
?>

And finally everything works fine.