PHP Image Resizer Class

class imageResizer {
var $newImage;

function getSquareImage($size, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_sq’. $size .’.jpg’;

//if that image does not exist already (we haven’t resized it before):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $size || $height > $size) {
if ($width > $height) $ratio = $size / $width;
else $ratio = $size / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

function getImageDimensions($sizeX, $sizeY, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_’. $sizeX .’x’. $sizeY .’.jpg’;

//if that image does not exist already (we haven’t resized it befoe):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $sizeX || $height > $sizeY) {
if ($width > $height) $ratio = $sizeX / $width;
else $ratio = $sizeY / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

}

PHP Image Downloader Class

a simple PHP class with just one method to download images from a remote server. checks for supported types (gif, jpg, png) and will throw an exception if the file is not supported.

usage:

  1. create instance if ImageDownloader
  2. call function downloadImageFrom with parameters: url, destination in local file-system, image name, image type
explanation of download image:
  1. check if the image type is supported
  2. get width and height of original image
  3. create a new image on local machine with width and height
  4. check the filetype and load correct image from imagecreatefrom~ function, parsing the image remote url
  5. resample the image
  6. save the image as the correct filetype using img~ function, parsing the image in memory and the new filename

code:

class ImageDownloader {
var $supported = array(“png”,”jpg”,”gif”);
function downloadImageFrom($url, $to, $fn, $img_type) {
if (in_array($img_type, $this -> supported)) {
list($width, $height) = getimagesize($url);
$newImg = imagecreatetruecolor($width, $height);
$imageTmp = ”;
if ($img_type == ‘png’) {
$imageTmp = imagecreatefrompng($url);
}
elseif ($img_type == ‘jpg’) {
$imageTmp = imagecreatefromjpeg($url);
}
elseif ($img_type == ‘gif’) {
$imageTmp = imagecreatefromgif($url);
}
if ($imageTmp != ”) {
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $width, $height, $width, $height);
$newPath = $to . $fn . ‘.’ . $img_type;
$this -> imgLoc = $newPath;
if ($img_type == ‘jpg’) {
imagejpeg($newImg, $newPath);
}
elseif ($img_type == ‘gif’) {
imagegif($newImg, $newPath);
}
elseif ($img_type == ‘png’) {
imagepng($newImg, $newPath);
}
}
}
else {
throw new Exception(‘Not supported file-type’);
}
}

UK and the EU – exit or not

in response to an article on the bbc website:

in my opinion, the referendum is quite a bad idea for the country, but popular in terms of pr. It also distracts from the otherwise poor performance of the coalition… (although i think a labour government would have been much more disastrous) 
the referendum puts the decision of an eu membership into the hands of people who do not know all the details and consequences of such a move. It’s easy to blame the eu for some of the less welcome legislations they force on it’s member states and we should all fight against that, but an exit of the eu might have a lot of unintended consequences when the uk won’t receive it’s benefits any more.
given that this country has a trade deficit, it is, in the long term, advisable to be part of a bigger community of countries where some of the countries have an export surplus. the eu is definitely a long term investment, which may only start to pay dividends in 25+ years. 
we might not like some laws that are imposed by eu, but we can and must diligently influence the decision making on eu laws and how eu laws will effect member states laws when there is conflict. a general rule of ‘eu law supersedes country law’ and the idea common employment laws, just causes grievance. we should aim to align laws and resolve conflicts on a case-by-case basis. over years we will iron out the issues between the laws, in the meantime we should just make humanitarian decisions to resolve conflicts.
But an eu exit is dangerous and would be reversed within living memory, no doubt. i’m trying to find an analogy to this problem, but it proofs difficult…

Hate when people “sign” emails, but you can’t reply

i really think it’s the worst!

the conversation about whether or not to sign emails by an officer of your company has been going on for some time. in principal i don’t have a strong opinion about this, but i have certain expectations from such ‘signed’ emails:

  1. the person that signs this message has read the content and approved it’s content
  2. the email does not come from a ‘no-reply’ address (an address that only sends and all replies are discarded)
  3. when a reply is received, the person that signs the email is aware of the response
i have worked in a few online organisations and i’ve seen the good, the bad and the ugly. surprisingly it’s the smaller organisations that seem to get it right. your emails aren’t signed by anyone, but when you reply a bunch of people, including the customer service and marketing teams still see your reply and answer to it.
the bigger companies however get it completely wrong: singing the communication, sending from a ‘no-reply’ address and then don’t even make the customer aware of the fact that the email won’t be received by any human being!
two of these companies are british gas and british telecom. funny enough, you can find the bt-guy on twitter: warren buckley. he actually replies there and told me that he signs it so people can find him and he does hear what the customer comments are. i have an idea for you warren: put your email address (or a secondary mailbox, if you want) on the communication you crooks send out – i’m sure that people would be happy to reply!

No more netbooks in future

as I read in the Guardian this morning, Asus (et all) are stopping the production of netbooks. It seems the economy of the netbook has failed. Too many tablets are being sold and the that has probably crippled the already poor performing PC hardware market.

It’s not even that the UltraBooks have moved into the market, no, it was partly notebooks, partly tablets and – most shockingly – the licensing that had to be paid to Microsoft for Windows XP or 7. These additional £30 – £70 meant that the netbook margin almost disappeared.

What a shame, as I really enjoyed having an inexpensive, small, durable, USB and SD card slot wielding, low energy consuming netbook when on the go. Thank god I still have one!

What I found most disappointing about the whole story was that this, kinda, seals the fate of the One Laptop Per Child campaign from a few years back. The Raspberry PI won’t be the same, as you still need big screens, keyboards and mice on each desk. I also don’t think that a tablet, least of all the iPad, would be a good addition to the classroom. It’s too simple, it’s not good for typing for a long time and it’s bound to be more restrictive than a computer in so many ways. You can’t teach children IT and programming on a tablet (yet).
The netbook would have the solution and meant that you could have run a complete system – like I already am – with IDEs, services (such as MySQL, Tomcat, Apache…) and that would have really made a difference in the classroom.

It’s a shame that this was possibly never recognised. Maybe a large scale production of netbooks for schools would have been an excellent solution and netbooks would have become even more affordable. After all, they are able to build tablets for under £100…

The amount that could have been saved on books and materials over the years would be massive.

I wonder if anyone ever looked into this.

Python on MS SQL (via pyodbc)

I have been struggling, albeit not very long, to set up a DB connection to a remote MS SQL server through Python. I thought that the process is not as straight forward as I’d like it to be, but I guess that’s okay because as far as I can see the best solution is to cobble it together a bit.

Basically it seems that the best solution is not to use a python end to end module as a driver, but to use features that are available otherwise, such as ODBC. Then use a ODBC driver for the MS SQL engine and then use ODBC within Python.
As a Linux person, I first had to install unixodbc:

sudo apt-get install unixodbc unixodbc-dev

This takes care of you being able to use ODBC to connect to ODBC database servers. The other part is having a ODBC driver for the connection to the MS SQL server. There are probably others and I shall list them in due time, but for MS SQL, I found it easiest to use FreeTDS (an implementation of the Tabular Data Stream for Linux).

To install:

sudo apt-get install freedts-dev tdsodbc

With this done, you can preceed to modifying the /etc/odbcinst.ini:

sudo nano /etc/odbcinst,ini

[ODBC]
Trace = Yes
TraceFile = /tmp/odbc.log

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
UsageCount = 1

Please note that the paths for libtdsodbc.so and libtdsS.so are not necessarily as the ones given in this example. For one, I’m using a 64bit version, so on 32 the path is probably different. Another possible location for the files would be: /usr/lib/odbc/.
Either search for the location or seek Google to find where your distribution saves these files.

You will also need to have the python-dev stuff installed:

sudo apt-get install python-dev 

Now that the system is installed, you will want to install the pydobc module. You can download the latest version of the pyodbc module here. Extract the file where it’s convenient for you and ‘cd’ into the folder.
To install simply do:

sudo python setup.py install

More and more sponsored content on facebook

I logged into facebook today to see how Myko was getting on yesterday (he was on a UK game show).

Unfortunately, facebook is taken over by useless ‘like’ messages. All I can see is this guy likes Heinz Beans, this girl like Burberry, someone else likes Coca-Cola, Amazon, Hotels.com…

Is there something wrong with me that I don’t care what sh*t you people like? Or something with other people that need to tell everyone when they like something so trivial and unimportant like a brand? it makes me wonder how much people really like beans? A lot more than me?

Or could it be facebook being incredibly desperate to get some kind of advertising revenue that they swamp us with this nonsense? Unfortunately I seem to be unable to opt-out of these ‘This c*nt likes that sh*t’ messages.

Am I the only person that is bothered by all the adverts that are being communicated out to me? The way that this is done is also incredibly subliminal – first there were few messages here and there, now it’s everywhere and meant to be adapted by us all.

I have never been a ‘fan’ of facebook, but I’m getting increasingly turned off by the whole ordeal.

Websites won’t work properly without cookies

Yet the European legislative that has passed a few weeks ago now make it mandatory to advise people how cookies are being used.
It’s like stopping every customer that enters your high street store and telling him how the CCTV system works, what they are going to do with your purchase and other information and only then letting them through…

Nevermind, protecting yourself from pesky third party advertising cookies is easy. Check this page to opt out of all sorts of networks that use behavioural and remarketing cookies.

This, btw, also works through cookies!

Olympic Glory in Broadcasting

Some post Olympic humour……

 
Here are the top nine comments made by NBC sports commentators during the London Summer Olympics that they would like to take back:

1. Weightlifting commentator: “This is Gregoriava from Bulgaria. I saw her snatch this morning during her warm up and it was amazing.”
 
2. Dressage commentator: “This is really a lovely horse and I speak from personal experience since I once mounted her mother.”
 
3. Paul Hamm, Gymnast: “I owe a lot to my parents, especially my mother and father.”
 
4. Boxing Analyst: “Sure there have been injuries, and even some deaths in boxing, but none of them really that serious.”
 
5. Softball announcer: “If history repeats itself, I should think we can expect the same thing again.”

6. Basketball analyst: “He dribbles a lot and the opposition doesn’t like it. In fact you can see it all over their faces.”
 
7. At the rowing medal ceremony: “Ah, isn’t that nice, the wife of the IOC president is hugging the cox of the British crew.”

8. Soccer commentator: “Julian Dicks is everywhere. It’s like they’ve got eleven Dicks on the field.”

9. Tennis commentator: “One of the reasons Andy is playing so well is that, before the final round, his wife takes out his balls and kisses them… Oh my God, what have I just said?”

Auto Download File From FTP – then rename

A little while ago my friend asked me to help him with some Windows commandline scripting. Basically he was waiting for ages for the development team in his company to sort this simple task out and asked me how he can every day automatically download a file from FTP, then rename it with the current date.

I don’t do Windows very much and Linux is just much better for exactly these things, but this seemed like an easy enough challenge.

In the end I didn’t manage to do everything in one file, but I think that two should work just as well when you add them to the Windows task scheduler…

Here are the files with some explanation:

downloader.bat:

@ftp -i -s:”%~f0″&GOTO:EOF
open ftp.someserver.com
[username]
[password]
!:— FTP commands below here —
mget “*.*”
disconnect

The first line starts the windows command line FTP client and the parameters essentially say that everything else in the file should be ignored by the command line itself, but be parsed to the FTP client.
Line two opens the connection and line three and four will send the authentication details. Line six gets all files in the current directory. You could really do here anything you want on the remote and local server, eg: change the local directory path (lcd …), the remote one (cd …) send files (put) or even delete them (rm). At the end, I simply disconnect.
renamer.bat:

for /f “tokens=1-5 delims=/ ” %%d in (“%date%”) do rename “SomeFileName.txt” SomeFileName_%%f-%%e-%%d.txt

In one line: I’m starting a for-loop and I use a tokeniser to delimit the following string into 5 (1-5) sections. %%d specifies the beginning character used for the token (d) and all that follow will be e,f,g…
then I use the %date% (in (“%date%”) function to get the current date. Now, In the do part, I will rename the file from SomeFileName.txt to SomeFileName_yyyy-MM-dd.txt
I hope this makes sense and helps a few people. Best of luck…