Archive for April, 2010
Javascript Const Note
by z3n on Apr.13, 2010, under Notes
In order to keep my code clean i wrote a javascript with const variable declarations, those were constants anyway and there were no reason to declare them as var, right?
Wrong, big mistake, IE don’t support consts right, turns out it gives javascripts errors (on the wrong position, of course), i will not even go deep into this, cuz it will just turn into more shit, so i’m just replacing const per var;
So, note to self, never use consts on javascript.
Fedora 10 Valid Repos
by z3n on Apr.07, 2010, under Linux Happyness, Tips & Hints
Problem:
My fedora 10 repos stopped working.
Solution:
My host had the dedicated configurated with a single static mirror. I figured out that by removing the baseurl from repos and enabling the mirrorlist it would work just fine.
Download:
PNG Watermarking images with PHP
by z3n on Apr.07, 2010, under Coding, Tips & Hints
Problem:
How to watermark images with png’s alphablending enabled?
Solution:
function image_overlap($background, $foreground, $overlapX=-1, $overlapY=-1) {
$insertWidth = imagesx($foreground);
$insertHeight = imagesy($foreground);
$imageWidth = imagesx($background);
$imageHeight = imagesy($background);
$overlapX = ($overlapX == -1) ? $imageWidth-$insertWidth-5 : $overlapX;
$overlapY = ($overlapY == -1) ? $imageHeight-$insertHeight-5 : $overlapY;
imagecolortransparent($foreground,imagecolorat($foreground,0,0));
imagecopymerge($background,$foreground,$overlapX,$overlapY,0,0,$insertWidth,$insertHeight,100);
return $background;
}
function watermark($src,$watermark,$src_format,$quality=75) {
if (file_exists($src)) {
_o("Watermarking: ".$src."...",1);
switch ($src_format) {
case "jpg":
$src_img=imagecreatefromjpeg($src);
break;
case "gif":
$src_img=imagecreatefromgif($src);
break;
case "bmp":
$src_img=imagecreatefrombmp($src);
break;
case "png":
$src_img=imagecreatefrompng($src);
break;
default:
die("Unknown format");
}
// prepare source
$src_size=getimagesize($src);
$img=imagecreatetruecolor($src_size[0],$src_size[1]);
imagecopyresampled($img,$src_img,0,0,0,0,$src_size[0],$src_size[1],$src_size[0],$src_size[1]);
@ imagealphablending($img,true);
// load watermark
$watermark_size=getimagesize($watermark);
$watermark_img=imagecreatefrompng($watermark);
@ imagealphablending($wartermark_img,true);
// apply watermark
$img=image_overlap($img,$watermark_img);
imagecopy($img,$watermark_img,0,0,0,0,0,0);
imagedestroy($src_img);
imagedestroy($watermark_img);
switch ($src_format) {
case "jpg":
imagejpeg($img,$src,$quality);
break;
case "gif":
imagegif($img,$src);
break;
case "bmp":
imagebmp($img,$src);
break;
case "png":
imagepng($img,$src,$quality);
break;
}
imagedestroy($img);
} else {
die("Watermark: Source don't exists -- ".$src);
}
}
Note:
For BMPs you will need the functions on this post.
Overriding MySql Max Connections
by z3n on Apr.06, 2010, under Tips & Hints
Problem:
MySQL has reached max connections and you can’t login, even being root.
Solution:
You can either edit my.cnf raising the limit and logging in or you can do this hack:
gdb -p $(cat /var/run/mysqld/mysqld.pid) \ -ex "set max_connections=5000" -batch
What’s the difference?
By changing my.cnf you will need to restart the server, if you got the limit reached long time ago lots of queued connections will instantly flood the server once you reset it, making impossible to access it within a few seconds. So you will need to keep restarting it over and over again until all the queue is gone. In some cases you can’t even restart it, server gets stuck trying to close connections, then you need to kill mysqld, but that can generate broken tables. Sometimes only a full system restart fixes the issue.
By changing the value with the hack, you will be able to raise the max connections without restarting mysqld, which allows you to login and check what’s going on, even run some scripts to clean up idle/stuck connections, making things easier.
Javascript Floating Point Issues
by z3n on Apr.01, 2010, under Coding, Tips & Hints
Problem:
Javascript floating point issues.
Solution:
It’s well known that javascript has major issues when it comes to precision math, even the simplest operations tend to be a nightmare. Although i was aware of that i suddenly found myself into another tricky thing with zeros on the left (eg: 025).
To avoid issues with javascript rounding issues I get all numbers converted to strings, stripping their floating point, this only works if all numbers are from the same precision or if you do a function do that, since there’s no rounding function (besides .toFixed) that does that proprietly.
So, in sum, 1.23 will be calculated as 123, eg:
var a = 1.23; var b = 4.56; a = parseInt(a.toString().replace(/\./g,"")); // 1.23 -> 123 b = parseInt(b.toString().replace(/\./g,"")); // 4.56 -> 4.56 var x = a + b; // = 579 x=x.toString(); return x.substr(0,x.length-2)+"."+x.substr(x.length-2);
Now that’s good, right…?? Apparently yes, but let’s do another example:
var a= 0.25; a = parseInt(a.toString().replace(/\./g,"")); // 0.25 -> 025 var x = 1*a; // 1* 025 = 025 -- right? js will return 021 return x.substr(0,x.length-2)+"."+x.substr(x.length-2);
When there’s a zero on the left, javascript goes nuts. An easy fix would be:
while (x.toString().substr(0,1) == "0")
x=x.substr(1);
and problem solved!
References:
ENLTV Bluetooth Issue
by z3n on Apr.01, 2010, under Tips & Hints
Problem:
How to get audio bluetooth playback with my Encode PC-TV (ENLTV-FM) card.
Solution:
My bluetooth device is old and simply don’t work with the newest versions of BlueSoleil, so i’m stuck in this 1.6.x version, which totally sucks. It don’t work right with some specific devices and it’s also got a lot of bugs.
Just to have an idea, as far as i know, the current version is 6.x.
So, because of random issues I can’t have LIVE audio playback comming from the MIC of my onboard card.
Enabling ENLTV Timeshift option resolved the issue.