Archive for August, 2009
jQuery + XML + IE = xmlDOM issue … or no??
by z3n on Aug.29, 2009, under Coding, Notes
Problem:
In the middle of the developing of a very complex script i figured out that IE was simply ignoring the xml documents i sent to it by ajax. Searching the web i’ve found this $.xmlDOM jQuery extension that is supposed to fix the IE issue with xmls. Although the extesion was clear and other people claims it work, it didn’t worked for me, how lucky is that?
Solution:
This took a while to solve, and i will skip all the boring process. Turns out that i didn’t needed the $.xmlDOM extension at all, the issue was on the xml. I’m developing this script in a language that has accents, i need to use `á` like html entities in order to avoid malfuncioning with data transport, so this little ampersand was breaking IE.
How nice is that?
JASC Paint Shop Pro 8-9 Issues
by z3n on Aug.26, 2009, under Tips & Hints
For as much as I like bleeding edge technologies, I tend to use old programs to do simple tasks, like, you don’t need photoshop cs4 to do a simple screenshot copy-paste, neither you don’t need to loose time on paint doing that so. Also cs4 gif transparency and gif optmizing is just awful terrible and lame.
I still using paint shop pro 9, even deprected and replaced by corel’s super crap photo-paint.
It turns out that this program don’t work right with non-english languages, it simply gives a message `no valid temp directory defined` and quits.
I fixed this before but i keep forgetting. This is how to do it:
Open system at control painel or if you are leet, windows + pause, go on advanced -> system variables -> set/add TMP and TEMP variables for your user into a `easy` path not that %USER% windows crapola, for me i just set both to d:\temp and that’s it. Now you can have fun with paint shop pro.
MSSQL Hell
by z3n on Aug.25, 2009, under Coding, lol
One might say that mssql is more powerfull and versatile than the other free choices around, this person should be slapped in the face repeated times.
MS SQL is the most awful and horrible SQL server ever.
Moving data to a server to another turns into a hell specially when you use the official microsoft tools to do that. Somehow, microsoft managed to put their legacy bugs on this product as well. The default `copy database` tool from SQL Management Studio 2008 comes factory broken. You can’t use it due some unknown mistake. The more i searched about the `Object reference not set to an instance of an object` issue the more crap i found about it. This is also a C# error message, making it harder to find on google, although many people discussed about this specific sql issue, nobody apparently found an fix it.
There’s some patches at microsoft site, but none of them worked.
So, I was tired of SQL Management studio when i found a tool to export/import sql databases, also from microsoft. Despite the timeouts, it worked right…. until, after 3 hours i found out that it didn’t copied the identity columns, which i specificly checked to copy at the program.
Back at Manager, i happly found that you can’t add identity columns without dropping the table and loosing all the data.
So, none of microsoft tools to manage microsoft sql server were able to do a simple database copy, what could be done with a single command line at mysql turns into a hell spiral because of microsoft great tools.
I was tired of it then i decided to look for a third party tool, and i found out MS SQL Maestro, which surprisingly only had a little bug on connection passwords, with this program i was able to do a script of the database, with the identity columns included, and now, at 4am i’m inserting it back on the new database, just hope i don’t fall into another happy error, since client will want his database working at 8am :)
Just easy as that.
.htaccess 401 note
by z3n on Aug.20, 2009, under Notes
401 redirects must be within the same domain without the full url:
ErrorDocument 401 /error_page.ext <– right
ErrorDocument 401 http://www.example.com/error_page.ext <– wrong, EVEN if your domain is the same that it’s being redirect to.
overflow reloaded
by z3n on Aug.19, 2009, under Notes
I’ve been using those crappy generic themes for a while, so today i decided to do a ripoff on pixeled and do my own theme.
Nothing so much amazing, just black and simple, the way i like to read in the dark.
jQuery hoverpulse different image ratio fix
by z3n on Aug.18, 2009, under Coding
The jQuery’s extension hoverpulse created allows you to have images that resize themselfs on a mouse hover, very usefull when you need to have larger thumbs without showing the full image like other extensions does.
However hoverpulse has a issue when it comes to resize non square images, it simply stretches them, so I wrote this little custom version of hoverpulse in order to have this gap filled:
Links:
little jQuery to loop through checkboxes
by z3n on Aug.18, 2009, under Coding
This is a little function to loop throught a specific form or id with checkboxes, it will check if the checkbox is checked and then you can `do something`.
-
function (what)
-
{
-
jQuery.each(jQuery("#"+what+" :checkbox"),function(){
-
if (jQuery(this).is(':checked')) {
-
// do something
-
}
-
});
-
}
Ultimate PHP Resize Function
by z3n on Aug.15, 2009, under Coding
In the last few days I’ve been developing some new stuff that required my old img_resizer php function to be refurbished, I also found a issue with wBMP not being the same as BMP format.
Apart from that i got some extra file checking and the ability of recognizing the file format independent of the extension (this will only work with exif extension, which when not enabled will fall back to a try and error little loop).
There’s also some new stuff added, when you don’t want to automatically crop an `out of ratio` resized image you can enable the $nocrop option which will resize the image to the right ratio without cropping or stretching it.
So this is the v2.7, not sure if it will be the last one, but so far it’s the ultimate.
-
-
function _o($x) { echo $x."’n"; }
-
-
function _ckdir($fn) {
-
if (strpos($fn,"/") !== false) {
-
$p=substr(substr($fn,0,strrpos($fn,"/")),0,250);
-
if (!is_dir($p)) {
-
mkdir($p,755,true);
-
}
-
}
-
}
-
function imagecreatefrombmp($p_sFile) { // by alexander@alexauto.nl at http://www.php.net/manual/en/function.imagecreatefromwbmp.php
-
// Load the image into a string
-
$file = fopen($p_sFile,"rb");
-
$read=fread($file,10);
-
while(!feof($file)&&($read<>""))
-
$read.=fread($file,1024);
-
$temp=unpack("H*",$read);
-
$hex=$temp[1];
-
$header=substr($hex,0,108);
-
-
// Process the header
-
// Structure: http://www.fastgraph.com/help/bmp_header_format.html
-
if (substr($header,0,4) == "424d") {
-
// Cut it in parts of 2 bytes
-
$header_parts=str_split($header,2);
-
-
// Get the width4 bytes
-
$width=hexdec($header_parts[19].$header_parts[18]);
-
-
// Get the height4 bytes
-
$height=hexdec($header_parts[23].$header_parts[22]);
-
-
// Unset the header params
-
unset($header_parts);
-
}
-
-
// Define starting X and Y
-
$x=0;
-
$y=1;
-
-
// Create newimage
-
$image=imagecreatetruecolor($width,$height);
-
-
// Grab the body from the image
-
$body=substr($hex,108);
-
-
// Calculate if padding at the end-line is needed
-
// Divided by two to keep overview.
-
// 1 byte = 2 HEX-chars
-
$body_size=(strlen($body)/2);
-
$header_size=($width*$height);
-
-
// Use end-line padding? Only when needed
-
$usePadding=($body_size>($header_size*3)+4);
-
-
// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
-
// Calculate the next DWORD-position in the body
-
for ($i=0;$i<$body_size;$i+=3) {
-
// Calculate line-ending and padding
-
if ($x>=$width) {
-
// If padding needed, ignore image-padding
-
// Shift i to the ending of the current 32-bit-block
-
if ($usePadding)
-
$i+=$width%4;
-
-
// Reset horizontal position
-
$x=0;
-
-
// Raise the height-position (bottom-up)
-
$y++;
-
-
// Reached the image-height? Break the for-loop
-
if ($y > $height)
-
break;
-
}
-
-
// Calculation of the RGB-pixel (defined as BGR in image-data)
-
// Define $i_pos as absolute position in the body
-
$i_pos=$i*2;
-
$r=hexdec($body[$i_pos+4].$body[$i_pos+5]);
-
$g=hexdec($body[$i_pos+2].$body[$i_pos+3]);
-
$b=hexdec($body[$i_pos].$body[$i_pos+1]);
-
-
// Calculate and draw the pixel
-
$color=imagecolorallocate($image,$r,$g,$b);
-
imagesetpixel($image,$x,$height-$y,$color);
-
-
// Raise the horizontal position
-
$x++;
-
}
-
-
// Unset the body / free the memory
-
unset($body);
-
-
// Return image-object
-
return $image;
-
}
-
function img_force($src,$quality,$w,$h,$saveas,$nocrop,$force) {
-
foreach (array("jpg","gif","png") as $x) { // will skip bmp formats
-
if ($x != $force) {
-
if (img_resizer($src,$quality,$w,$h,$saveas,$nocrop,$x)) {
-
return 1;
-
break;
-
}
-
}
-
}
-
return 0;
-
}
-
function img_resizer($src,$quality,$w,$h,$saveas,$nocrop=0,$force=0) { /* v2.71 with auto crop, no crop, automatic file type detection when avalible (requires exif library) */
-
-
if (!is_file($src)) { _o("Source is not a file.");return 0; }
-
if (!is_readable($src)) { _o("Can't read: ".$src);return 0; }
-
if ((file_exists($saveas)) && (!is_writeable($saveas))) { _o("Can't write to: ".$saveas);return 0; }
-
-
$r=1;
-
-
if (function_exists('exif_imagetype')) { // automatic filetype detection
-
$e=exif_imagetype($src);
-
if ($e == IMAGETYPE_JPEG) {
-
if (!$OldImage=@ImageCreateFromJpeg($src)) { $r=0; }
-
} elseif ($e == IMAGETYPE_GIF) {
-
if (!$OldImage=@ImageCreateFromGif($src)) { $r=0; }
-
} elseif ($e == IMAGETYPE_BMP) { // custom
-
if (!$OldImage=@ImageCreateFrombmp($src)) { $r=0; }
-
} elseif ($e == IMAGETYPE_WBMP) {
-
if (!$OldImage=@ImageCreateFromwbmp($src)) { $r=0; }
-
} elseif ($e == IMAGETYPE_PNG) {
-
if (!$OldImage=@ImageCreateFromPng($src)) { $r=0; }
-
} else {
-
_o("Not a Valid Image! (".$e.") — ".$src);$r=0;
-
}
-
} else { // filename based filetype detection, might be subject to incorrect file extensions, resulting on a error, which when enabled will fall into a try and error `force` mode
-
$e=($force == "0") ? strtolower(substr($src,strrpos($src,".")+1)) : $force;
-
if (($e == "jpg") || ($e == "jpeg")) {
-
if (!$OldImage=@ImageCreateFromJpeg($src)) { $r=0; }
-
} elseif ($e == "gif") {
-
if (!$OldImage=@ImageCreateFromGif($src)) { $r=0; }
-
} elseif ($e == "bmp") { // this is NOT wbmp
-
if (!$OldImage=@ImageCreateFrombmp($src)) { $r=0; }
-
} elseif ($e == "png") {
-
if (!$OldImage=@ImageCreateFromPng($src)) { $r=0; }
-
} else {
-
_o("Not a Valid Image! (".$e.") — ".$src);$r=0;
-
}
-
}
-
-
if ($r) {
-
list($width,$height)=getimagesize($src);
-
// check if ratios match
-
$_ratio=array($width/$height,$w/$h);
-
if ($_ratio[0] != $_ratio[1]) { // ratio don't match
-
if ($nocrop) { // since we can't crop we will need to do image on a different height/width
-
$hx = (100 / ($width / $w)) * .01;
-
$hx = @round ($height * $hx);
-
-
$wx = (100 / ($height / $h)) * .01;
-
$wx = @round ($width * $wx);
-
-
if ($hx < $h) {
-
$h = (100 / ($width / $w)) * .01;
-
$h = @round ($height * $h);
-
} else {
-
$w = (100 / ($height / $h)) * .01;
-
$w = @round ($width * $w);
-
}
-
} else { // crop image
-
// find the right scale to use
-
$_scale=min((float)($width/$w),(float)($height/$h));
-
-
// coords to crop
-
$cropX=(float)($width-($_scale*$w));
-
$cropY=(float)($height-($_scale*$h));
-
-
// cropped image size
-
$cropW=(float)($width-$cropX);
-
$cropH=(float)($height-$cropY);
-
-
$crop=ImageCreateTrueColor($cropW,$cropH);
-
ImageCopy($crop,$OldImage,0,0,(int)($cropX/2),(int)($cropY/2),$cropW,$cropH); // crop the middle part of the image to fit the thumbnail's proportions
-
}
-
}
-
-
// do the thumbnail
-
$NewThumb=ImageCreateTrueColor($w,$h);
-
if (isset($crop)) { // been cropped
-
ImageCopyResampled($NewThumb,$crop,0,0,0,0,$w,$h,$cropW,$cropH);
-
ImageDestroy($crop);
-
} else { // ratio match, regular resize
-
ImageCopyResampled($NewThumb,$OldImage,0,0,0,0,$w,$h,$width,$height);
-
}
-
_ckdir($saveas);
-
ImageJpeg($NewThumb,$saveas,$quality);
-
ImageDestroy($NewThumb);
-
ImageDestroy($OldImage);
-
} elseif (($force == "0") && (!function_exists('exif_imagetype'))) {
-
$r=img_force($src,$quality,$w,$h,$saveas,$nocrop,$e);
-
}
-
return $r;
-
}
Keep it simple, stupid jQuery experience
by z3n on Aug.14, 2009, under Coding, Notes
As much as I like jQuery I must admit that it’s far away from simplifying things. Although it might be a great idea using it on 100% jQuery scripts, it’s a real bad idea using it to refurbish an old script.
Today I’ve spent over 2 hours implement jQuery on a old script I have, and I felt into so many issues that it didn’t worth at all.
My script was simple, I had a huge variable list that could be edited by a form, script loops through the variables building a form with input fields for each variable. I will not get into specific details because it’s boring, but I needed to allow the user to add a new variable inside an array, so i thought that jQuery would help a lot since i only would need to dynamic add a new input field as needed then post everything back to script to save the file.
First I spent an hour figuring out that jQuery was ruining the text by converting the whole thing into UTF-8, loosing all the accents, eventually I found out about contentType encoding ajax variable:
contentType:"application/json; charset=utf-8"
which could be changed to the charset i wanted.
It was useless, jQuery still posting into the wrong charset, there’s some other tweks on this, but they are also useless.
I was able to fix the accent issue with this php statement:
mb_convert_encoding(urldecode($variable),”ISO-8859-1″,”auto”);
This is much more obscure though, but I was familiar with it since i coded in japanese charsets which are a pain to convert.
After having this cleared, and searching a lot of useless blogs and postings, turns out that jQuery was using the hard coded form names to post the data, which could be overlapped by an dynamic added field, I did a script to change the name of the hard coded inputs, something like this:
$(“#field_id”).attr(’name’,’new_name’);
Theorically, it worked, but when I did:
$(“#form”).serialize();
jQuery used the dynamic fields with the ordinary hard coded ignoring the attr changes.
Now I had to add a handler to dynamic convert and read all the inputs and do my own serialize in order to TRY to make it work…and that’s because i didn’t tested it on IE yet.
So that’s when I quit using jQuery for this script and do something plain and simple, which took me about 20 minutes and 0 searches.
It looks like that if I had used DOM elements for the whole form, all elements generated by jQuery itself, not hard coded, i would have less trouble with the form, although, the charset issues still.
Super Fun Sources:
jQuery Ajax Documentation (completly useless since contentType explanation has 2 lines)
MySQL procedure related functions
by z3n on Aug.05, 2009, under Notes
Since i keep forgetting this I will post as reminder:
show procedure status;
Lists procedures stored on current database.
show create procedure database.procedure_name;
Shows the source of the stored procedure.
grant execute on procedure database.procedure_name to username;
Grants execute privileges to the specific procedure and user.
Those are somehow obscure functions on mysql documentation, I think that they are still improving the procedures usability.