Hate My Blog

New Location. HateMyBlog.com

After realizing that A) this site doesn’t eat that much bandwidth, and B_ WordPress.com won’t allow many of the modifications and changes I need done, ive decided to host the blog my self at http://HateMyBlog.com/. All post have been moved there. Please update your bookmarks ;)

So….. just click here if you are unsure of what to do!

October 9, 2009 Posted by | Uncategorized | Leave a comment

Allowing Mixed Function Arguments with PHP

Some times you need function to work on a string, or maybe and array of strings. While just coding it to expect a string, with a little more code, you can use a little ingenuity to have it work doubly so.

I whipped up this little example:

function do_it($what){
	if(is_array($what)){
		foreach($what as $x)
			do_it($x);
		return;
	}
	echo "I am $what\n";
}
do_it("working");
do_it(array("running","sleeping","eating"));

gives us

I am working
I am running
I am sleeping
I am eating

a somewhat more complex example below. This will multiply $a & $b into another array of $z.

function ab_mult($a,$b,&$z){
	if(is_array($a)){			//is var $a an array?
		foreach($a as $x)		//loop thru it
			ab_mult($x,$b,$z);	//call its self with the 1 $a var, and leaving $b untouched.
		return;
	}
	if(is_array($b)){			//this is the catch for $b
		foreach($b as $y)
			ab_mult($a,$y,$z);
		return;			//in our case we must return after the loop!!!
	}
	//this is where your actualy function should begin
	$z[] = $a.$b;
}

$a=range('a', 'z');
$b=range(0, 9);

$z=array();
ab_mult($a,$b,$z);

var_dump($z);

September 16, 2009 Posted by | Code, PHP | , , , , , , | Leave a comment

Drawing the reddit alien with PHP and GD image library

Played hookie to my databases class and made this instead. Gotta love college.

reddit alien made with php

reddit alien made with php

If you make changes I would love to see the output.

//this code with generate a reddit alien in php code.
//its a big mess of numbers, deal with it.
//it generates a .png w/ transparency of the alien 200x180
//https://hatemyblog.wordpress.com/2009/09/10/drawing-the-reddit-alien-with-php-and-gd-image-library/


$im = @imagecreatetruecolor(200, 180);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$red= imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

//give it a defualt transparent bg.
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

//feet
imagefilledarc  ($im, 100, 150, 50, 20, 180, 0, $black,IMG_ARC_PIE);
imagefilledarc  ($im, 100, 152, 44, 20, 180, 0, $white,IMG_ARC_PIE);
imagelinethick($im,75,151,124,151,$black,2);

//arms
imagefilledellipse($im, 100, 105, 75, 60, $black );
imagefilledellipse($im, 100, 105, 68, 55, $white );

//ears
imagefilledellipse($im, 135, 49, 18, 18, $black );
imagefilledellipse($im, 135, 49, 13, 13, $white );
imagefilledellipse($im, 65, 49, 18, 18, $black );
imagefilledellipse($im, 65, 49, 13, 13, $white );

//atenna
imagelinethick($im,95,40,105,20,$black,4);
imagelinethick($im,105,19,118,20,$black,4);
imagefilledellipse($im, 125, 22, 15, 15, $black );
imagefilledellipse($im, 125, 22, 8, 8, $white );

//body
imagefilledellipse($im, 100, 100, 60, 100, $black );
imagefilledellipse($im, 100, 100, 55, 95, $white );
//head
imagefilledellipse($im, 100, 60, 80, 50, $black );
imagefilledellipse($im, 100, 60, 75, 45, $white );
//eyes
imagefilledellipse($im, 85, 53, 10, 10, $red);
imagefilledellipse($im, 115, 53, 10, 10, $red);
//mouth
imagearc($im, 100, 60, 50, 30, 25, 155, $black );
imagearc($im, 100, 61, 50, 30, 25, 155, $black );
imagearc($im, 100, 62, 50, 30, 25, 155, $black );

//save it
imagepng($im,'reddit.png');
imagedestroy($im);
echo '<img style="background:blue;" src="reddit.png">';



//helper function i ripped from
//
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
    /* this way it works well only for orthogonal lines
    imagesetthickness($image, $thick);
    return imageline($image, $x1, $y1, $x2, $y2, $color);
    */
    if ($thick == 1) {
        return imageline($image, $x1, $y1, $x2, $y2, $color);
    }
    $t = $thick / 2 - 0.5;
    if ($x1 == $x2 || $y1 == $y2) {
        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
    $a = $t / sqrt(1 + pow($k, 2));
    $points = array(
        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
        round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
    );
    imagefilledpolygon($image, $points, 4, $color);
    return imagepolygon($image, $points, 4, $color);
}

September 10, 2009 Posted by | Code, PHP | , , , , , , , | 2 Comments

Fuzzy status of who’s online at reddit

This is some quick and dirty little code to test without much precision if any particular user is online over at Reddit.

$url=’http://www.reddit.com/user/HattoriHanzo.json&#8217;;

//get the url | json decode it to an array
$j=json_decode(file_get_contents($url),1);
//this just grabs the first element and then its timestamp
$t=$j[data][children][0][data][created_utc];

//this is a delay of 15 min. use std. timestamp math
if((time()-$t) < (60 * 15)){ //and they are online ;) } //or offline ;( [/sourcecode]

September 10, 2009 Posted by | Code, PHP | , , , , | 1 Comment

lets improve /r/php

Dear /r/php;

I don’t always like to admit that the majority of my programming these days is still php, but its true. And I feel that the php community over here on reddit is somewhat thirsty for more. So lets all please try and do these few things to help feed the php family.

1. Browse /r/PHP/new/ more often
2. Upvote the good, ignore if you can the bad
3. Ask more questions
4. Answer & Share more good PHP

So that’s one thing im going to try and help push with this new Blogging experiment.

Expect to hear from me again ; )

DrMr Villain
Outtie 5000

September 9, 2009 Posted by | PHP | , | Leave a comment

php watermark and resize image

So I have this friend that likes to take photos, well yea hes a photographer, and apparently one of the lamest parts of his job is the prep work after the show. Basically he spends a lot of time resizing and adding his logo to some 100s of photos each next day after parties. And as I’m currently building an online gallery for him, I wanted to make his life simpler in this aspect.

This is code ripped out of the upload script, then i added some comments to explain whats going on, and then starting writing this post. I haven’t tested it, but it should work.

UPDATE: well wordpress butchers code, i put it on [pastebin.ca] http://pastebin.ca/1557910


$srcimg = "thebigphoto.jpg";    //the source file
$tmimg = "sm_".$srcimg;            //were to place the thumb
$wmimg = "watermark.pbr";        //the watermark files
$target  = 150;                    //thumbnail target size
$waterlevel = 40;                //the level of opacity

//load the jpeg and get its sizes
$image = imagecreatefromjpeg($srcimg);
$size = getimagesize($srcimg);

//this if just figures out what side is dominant
if ($size[0] &gt; $size[1]) {
$percentage = ($target / $size[0]);
} else {
$percentage = ($target / $size[1]);
}
//calc the thumb sizes
$newwidth = round($size[0] * $percentage);
$newheight = round($size[1] * $percentage);

//make a new img | copy the smaller | save as jpeg | clean up
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1]);
imagejpeg($thumb,$tmimg);
imagedestroy($thumb);

//make watermage img | and get its sizes;
$watermark = imagecreatefrompng($wmimg);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

this will place it Bottom-Left, 5px from the border
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

//copy it over | save it over the original | and clean up
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $waterlevel);
imagejpeg($image,$srcimg );
imagedestroy($watermark);
imagedestroy($image);

September 8, 2009 Posted by | Code, PHP | , , , , , , , | 1 Comment

Go Hate My Blog

I’m going to keep this short, as you can tell from the Hello World just below, this is a new blog pit of death.

See you on the other side!

September 6, 2009 Posted by | Uncategorized | Leave a comment

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

September 6, 2009 Posted by | Uncategorized | 1 Comment