Resizing / converting images in php

resize, imagemagick, convert, images, php, tips

One great (third party) tool to resize your images if available is imagemagick's command line tool: convert,  with many options and features. Here is an example:

passthru("convert -normalize -background white -quality 95 -resize x768\> -resize 1280x\> -support 0.8 +repage -interlace plane $tmpFile $destFile");

(will convert an image with maximum width 1024 or maximum height 768)

Now how about if this tool is not available?

Php can also be used to create and manipulate image files if compiled with the GD library (read more here)
So here comes a handy function that can be used for resizing both thumbnails and normal size images:


function ResizeImage($originalImage, $toWidth, $toHeight, $filename, $thumb = false)
{
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale = $width/$toWidth;
    $yscale = $height/$toHeight;

    // Recalculate new size with default ratio
    $offsetX = 0;
    $offsetY = 0;
    if ($thumb)
    {
        $new_width = $toWidth;
        $new_height = $toHeight;

        $ratioComputed = $width / $height;
        $cropRatioComputed = (float) 1 / (float) 1;

        if ($ratioComputed < $cropRatioComputed)
        { // Image is too tall so we will crop the top and bottom
            $origHeight= $height;
            $height= $width / $cropRatioComputed;
            $offsetY= ($origHeight - $height) / 2;
        }
        else if ($ratioComputed > $cropRatioComputed)
        { // Image is too wide so we will crop off the left and right sides
            $origWidth= $width;
            $width= $height * $cropRatioComputed;
            $offsetX= ($origWidth - $width) / 2;
        }

    }
    else
    {
        if ($yscale>$xscale)
        {
            $new_width = round($width * (1/$yscale));
            $new_height = round($height * (1/$yscale));
        }
        else
        {
            $new_width = round($width * (1/$xscale));
            $new_height = round($height * (1/$xscale));
        }
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0,  $offsetX, $offsetY, $new_width, $new_height, $width, $height);

    return (imagejpeg($imageResized, $filename));
}

PHP usort: sort an array of records using a particular sequence

sorting, array_search, array, usort, sort, tip, php

With PHP's usort function you are sorting an array using a custom function you have made:

function sort_by_title($a,$b)
{

  return strcasecmp($a['title'], $b['title']);

}
$myArray[0]['id'] = 1;
$myArray[0]['title'] = 'Hello';
$myArray[1]['id'] = 5;
$myArray[1]['title'] = 'Foo';
$myArray[2]['id'] = 6;
$myArray[2]['title'] = 'Foo etc';
.... and so on

usort($myArray, 'sort_by_title');

How about sorting the same array using a particular sequence of either ids, titles or whatever field of the record (using an array)?

Here is the solution:

(this example sorts the above array by a sequence that refers to the title field)

function sort_by_seq($a,$b)
{

  $seq = array('Foo', 'Hello', 'Foo etc');
  return array_search($a['title'], $seq) - array_search($b['title'], $seq);

}
usort($myArray, 'sort_by_seq');


Check out also another (sql related) tip for sorting sql result rows:

http://programming.pblogs.gr/general-tips.html which will be updated continuity with new tips.

Firefox add-ons

add-ons, firefox, web development, web

Back to posts...img1.jpg


So I just made my first Firefox add-on: 'Bookmarks menu'
More on that here.


A good place to start is Mozilla developers center and also "How to develop a Firefox extension"
What you will need is some html, javascript and XUL (XML User Interface Language) knowledge in order to make a Firefox add-on, time and a lot of Firefox restarts.
Well you may avoid the last since there is a helpful add-on called "Extension Developer".


Also recently Mozilla launched Jetpack an early beta of a project that will allow any web developer to build an extension easily as building a web site

Pingbacks

Unclassified

Well markp added pingbacksto our pblogs. It kinda replaced trackbacks.


So checking if it works!

Blocksum

freeware, puzzle, games

0111.jpgBlocksum is a great freeware puzzle game I discovered a few days ago.

Really addictive!

New pathfinderzzz

pathfinder

After Patfhinder Avatars, now we have a new Pathfinder Signup page!

That page uses real time checking for the user name the user picks does not exist and also for the rest fields required for the sign up process.

DirectX programming

cpp, directx, programming

At my previous programming era, I did some DirectX programming
with Borland Delphi (pascal language), building at that time Tetricom (a tetris - TetriNet clone).
Now I decided to get involved with C++ and DirectX (having already some
expirience with C++ at my work).
With joy I learned that Visual C++ is available as a free download!

So I downloaded and installed the following (all free downloads):


After some trials, failures and google searches I managed to compile succesfully my
first DirectX program in C++!

Unreal engine 3

unreal engine, unreal

The first Unreal Engine 3 title for PC has just been released as BeyondUnreal reports and it's name is RoboBlitz.

Also RoboBlitz includes a customized version of Unreal Editor of UE3!

Hopefully I will find a demo of the game to see Unreal Engine 3 in action before Unreal Tournament 2007!

Dream Pinball 3D

sudoku, pinball, games

Lately I discovered a great pinball video game called Dream Pinball 3D that is really a dream!:) with great graphics and game play.

Playing it I remembered Pinball fantasies another great pinball game (first released in Amiga those days) I used to play in DOS era and I decided to try it again using DOSbox. Well since I had not tried DOSbox before I can say the result was very good with this game.

Also if you like Sudoku, play it here on line (very good site and also great Js programming).

Firefox rulez

firefox, web development

As a web developer I always use Firefox and one of it's great features is extensions. Extensions is actually plug-ins that anyone with some programming skills can write, and so there are so many extensions out there.

One extension that I thought today is one that you could edit (and then save & view locally) the HTML source of a site. Well even this one exists (and I thought that I could involved with extension writing my self... well who knows I may will)

Some extensions I use at work are Tab mix plus, HTML validator, ie tab and other I can't remember right now (I'm at home) and Adblock here at home.


Firefox is really powerful... it can be transformed to a another application with the power of its web engine. www.ajaxlaunch proves it. Really impressive!

Waiting for Firefox 2... (Mozillazine says that beta 2 Milestone has been released)