Monday, September 19, 2011

HTML5 Video Scaling

According to w3c specs:
The dimension attributes are not intended to be used to stretch the image.
So, how do we scale the actual video? In a project that I'm working on (at home), I wanted to have an HTML5 video element that takes up the entire width and height of the browser window and resized whenever the user resized their browser. Here's what I came up with (using Yii as my PHP framework): Page script:
// you could bind to the equivalent event as well, of course
<video onloadeddata="resizeViewport()" id="background">
    <source src="<?php echo Yii::app()->request->baseUrl?>/assets/video/ed.ogv" />
</video>
.css:
#background
{
    position: absolute;
    -moz-transform-origin: 0 0;
}
.js (jQuery):
$(document).ready(function(){
});

$(window).resize(function(){
    resizeViewport();
});

function resizeViewport()
{
    var d = [$('#background').attr('videoWidth'), $('#background').attr('videoHeight')];

    $('#background').css('-moz-transform', 'scale('+
        $(window).width()/d[0]+','+
        $(window).height()/d[1]
        +')');
}
The scale CSS function is meant to change the dimensions of the image (as opposed to width and height). The other trick here is to ensure that the video has actually loaded so I can extract correct dimensions (which is only known once it's been loaded). Note: The code samples here are intended for FF only (each user agent has a different transform method).

Friday, September 16, 2011

Running Django unit tests

Not being a DBA, or someone who's even really familiar with database administration, I've been banging my head against the wall trying to get unit tests running on my dev box. I'd checked a few posts on SO, but it seems that I'd missed the one most important thing: RTFM.

Got an error creating the test database: permission denied to create database

If you're seeing the above error when running ./manage.py test, read on..

As Django creates and destroys a database when running unit tests, the user that you're using to connect to the server with needs to have permissions to create databases. This cannot be done by granting superuser permissions to the database user (within postgres of course) as the user needs to have database creation by default (you have to SET ROLE when using this method). To grant permissions to create a database by default, you should do this when creating the user account within psql:

create user myuser createdb

No more issues running unit tests.

Setting up DLink DWA-125 (USB 802.11N) on FreeBSD

So, my wife decided that she didn't like the computer and desk where they were (not that I blame her, I actually agreed), and proposed a move into another room of the house. After it was moved (she did it while I was at work one day), I definitely did like the placement better. But what did this mean for me? I either had to: a) Figure out a nice wiring solution as the router that I was previously wired to was in two rooms away, or b) Pick up a wireless card Picking up a wireless card was the obvious choice as it meant less work for me, or so I thought ;) This is more of a reminder for myself in case I have to do this again, but if it helps anyone else out, great. I ended up picking up a USB 11N stick (DWA-125 for those interested). I had forgotten to check whether or not the chipset had FreeBSD ported drivers prior to ripping the box open. Thankfully, there were drivers available. To get it up and running, here are the steps that I took: (Note, I use the run driver because of the DWA-125 chipset)

1) Added this to /etc/rc.conf:

wlans_run0="wlan0"
ifconfig_wlan0="WPA DHCP"

(I have WPA encryption on my router, so the "WPA" entry before "DHCP" is required.)

2) Added to /boot/loader.conf

if_run_load="YES"
runfw_load="YES"

3) Rebooted (you could also just kldload

4) Because I'm running WPA, I had to create /etc/wpa_supplicant.conf and add the following lines:

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
network={
ssid="myssid"
psk="mypassword"
}

5) Once this was all done, I ran wpa_supplicant to ensure everything was working properly:

wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf

And finally...

6) Reboot. And once again, I could listen to last.fm. I'll most likely end up compiling these options into the kernel, but this will do for now. Is it a good thing when you feel happy that you've set up and configured a USB network device?

Optimizing Your Web Apps (LAMP)

Note: This article assumes single server deployment and therefore doesn't cover topics such as load balancing

Code Profiling

Use xdebug's profiling option. This will give you insight into areas of your code that may need optimization.

SQL Profiling

Most of my projects use PostgreSQL, so I haven't actually used this myself, but here's some documentation on profiling your MySQL queries. Again, it'll give you insight into areas of your SQL scripts that could use some optimization.

Investigate Cache Options

If you have a lot of database queries on your pages or a lot of PHP logic in your pages, it can be expensive to generate. There are a few different methods of cacheing and, while each method helps to solve load times, each aims to solve different specialized problems: