Monday, November 10, 2014

PHP Packing Solution - How to divide items equally

This tutorial concept is actually come from stackoverflow where the solution was suggested in ruby script but I had translate it in PHP script.

$list_of_bags = array(11, 41, 31, 15, 15, 66, 67, 34, 20, 42, 22, 25);
# total weight of all bags
$weight_of_bags = array_sum($list_of_bags);
# how many containers do we have at our disposal?
$number_of_containers = 4;
# How much should one container weight?
$weight_per_container = $weight_of_bags / $number_of_containers;
# We make an array containing an empty array for each container
$containers[] = array();

$total = 0;

# For each bag
foreach ($list_of_bags as $bag) {
    for($i=0; $i<$number_of_containers; $i++){

        $total = (isset($containers[$i])) ? array_sum($containers[$i]) : 0;
        if($total + $bag < $weight_per_container){
            $containers[$i][] = $bag;
            break;
        }
    }
}

# output all containers with the number of items and total weight
foreach ($containers as $index=>$container) {
   echo "container $index has ";
   echo count($container);
   echo " ";
   echo "items and weigths: ";
   echo array_sum($container);
   echo "
";
}
Output:
container 0 has 3 items and weigths: 83
container 1 has 3 items and weigths: 96
container 2 has 2 items and weigths: 87
container 3 has 2 items and weigths: 76
container 4 has 2 items and weigths: 47

Wednesday, November 5, 2014

PHP Recursive Function

Tutorial by: Adam Lim


For example we need to create calculation as below:
5 => (5*2)+(4*2)+(3*2)+(2*1)+(1*1)

It can be resolved by for loop as below:

$total=0;
for($i=5; $i=0; $i--){
  $total += $i * 2;
}


To create a recursive function, it should as follow:

function test($int){
  if($int>0){
    echo $int.<br>;
    return(($int*2) + test($int-1));     // call back self function to continue loop
  }else{
    return 0;
  }
}

echo test(5);

Output:
5
4
3
2
1
30

DEMO

Dynamic Photo Framing API

Today bonus is to introduce photo framing api(apisilo.com) for framing industry. It is easy to use as you just need to pass four parameters which is art, frametop, framebtm, and framesize.

Parameter values and file format:
art - Photo or the artwork you wish to frame (only jpg, gif, png file format)
frametop - small piece of frame image, please see sample. (only jpg, gif, png file format)
framebtm - small piece of frame image, please see sample. (only jpg, gif, png file format)
framesize - integer value that you wish to control frame thickness.

How to use it:

Use with jquery getJSON to call API

$.getJSON("http://frame.apisilo.com/index.php?jsoncallback=?",
    {
      art: "http://www.tourismeoutaouais.com/blogue/wp-content/uploads/2014/10/halloween2-620x350.jpg",
      frametop: "http://www.imagemagick.org/Usage/thumbnails/blackthin_btm.gif",
      framebtm: "http://www.imagemagick.org/Usage/thumbnails/blackthin_btm.gif",
      framesize:"50"
    },
    function(data) {
        var str = data['result'];
        var finalData = str.replace(/\\/gi, '');

        $("#framed_artwork").attr("src",finalData);
    });

Append the result in framed_artwork as following:
<img id="framed_artwork" src="sample.jpg" />


DEMO