The Important Bit!

Latest Articles...

Easy way to get all .spanx divs aligned with row-fluid

April 9th, 2013

I was recently trying to find a nice solution to make sure all my span6 boxes were the same height as each other.

I found this solution, which worked on ALL of them:

boxes = $('.span6');		
maxHeight = Math.max.apply(
  Math, boxes.map(function() {
    return $(this).height();
}).get());
boxes.height(maxHeight);

This would make ALL of the .span6 boxes the same height… but that wasn’t quite what I wanted! The below example does what I needed:

$(document).ready( function() { 
		
		$('.row-fluid').each( function() {
				var this_height = $(this).height();
				$(this).children(".span6").each( function() {
					$(this).height(this_height);
				});
		});
	});

It assumes a setup of:

<div class="row-fluid">
    <div class="span6">some contents</div>
    <div class="span6">some other contents</div>
</div> 

<div class="row-fluid">
    <div class="span6">some contents</div>
    <div class="span6">some other contents</div>
</div> 

<div class="row-fluid">
    <div class="span6">some contents</div>
    <div class="span6">some other contents</div>
</div>

etc

You can do it using other sized boxes, such as:

<div class="row-fluid">
    <div class="span3">some contents</div>
    <div class="span3">some contents</div>
    <div class="span3">some contents</div>
    <div class="span3">some contents</div>
</div> 

Just be sure to update the .span6 in my above code to match your new .span3

I hope this helps someone else :)

Posted in jQuery | Tagged , , | Leave a comment

 

Adding watermarks to image in Perl

December 16th, 2012

I was writing a little script to watermark some images on my site, and thought I would share it for anyone who can make use of it :)

All the main options are found at the top – so should be pretty simple to customize. The idea behind this system is that you upload it to your /cgi-bin (either password protected, or just somewhere safe), and then access it from your browser. You then upload your image, and it will scale the image and add your watermark to it. Simple as that!

#!/usr/local/bin/perl

use warnings;
use strict;
use CGI;

my $write_folder = "/var/home/user/www/watermarked";
my $write_url    = "/watermarked"; # you can use the URL, or relative is just fine...
my $watermark_img = "/var/home/user/cgi-bin/cgi-bin/watermark/watermark.png";
my $transparency = "15"; # percentage of watermark image opacity
my $resize       = "400x400";

my $IN = new CGI;

print "Content-Type: text/html \n\n";

if (!$IN->param('doit')) {

	print qq|
	<form action="watermark.cgi" method="post" enctype="multipart/form-data">
		<input type="file" name="image" />
		
		<br /><br />
		<input type="submit" value="Watermark" >
		<input type="hidden" name="doit" value="1" />
	</form>
	|;

} else {
	
	my $file = $IN->param('image');
	my $ext = lc((reverse split /\./, $file)[0]);
	my $upload = $IN->upload('image');
	
	#print "GOT: $file , and $ext";
	
	my $write_file = CORE::time() . ".$ext";
	
	#print "Writing to $write_folder/$write_file <br />";
	
	open (UPLOADFILE, ">$write_folder/$write_file") || die $!;
		binmode UPLOADFILE;
		 while ( <$upload> ) {
			print UPLOADFILE;
		}
	close(UPLOADFILE);
	
	my $command = qq|convert $write_folder/$write_file -resize $resize $write_folder/$write_file|;
	my $command2 = qq|composite -dissolve $transparency -tile $watermark_img $write_folder/$write_file $write_folder/$write_file|;

	`$command`;
	`$command2`;
	
	if ($ext !~ /jpg/i) {
		my $new_filename = $write_file;
		   $new_filename =~ s/\.$ext/jpg/g;
		`convert $write_folder/$write_file $write_folder/$new_filename`;
		$write_file = $new_filename;
	}
	
	print qq|<img src="$write_url/$write_file" />|
	
}

Enjoy!

Posted in Perl Coding | Leave a comment

 

Convert number into

November 10th, 2012

If you want to convert a US phone number into the format xxx-xxx-xxxx, then you can use this nice simple function.

sub convert_tel {
	my $tel = $_[0];
	if ($tel =~ /^\d+$/) {
		$tel =~ s/([\d]{3,3})/$1-/g;
		$tel =~ s/\-$//g;
		return $tel;
	} else {
		return $tel;
	}
}

That would convert a 10 digit number into 123-123-123-1. However, it would be nicer to have it formatted as xxx-xxx-xxxx, so to get that lets do this regex instead:

$tel =~ s/([\d]{3,3})([\d]{3,3})([\d]{3,4})/$1-$2-$3/g;

Simply call with:

print convert_tel("123412341234");

Thats about it =)

Posted in Perl Coding | Tagged , , , | Leave a comment