Thursday, May 31

Resize Images with GD2

I found a bunch of places on the internets that help you with scripts to resize your images with gd2. These are all pretty cool because they resize your images while preserving their aspect ratio. Well I didn't want that so I had to read through the gd2 info on php.net. It's actually fairly simple, once I figured out what I was doing. I'm using a file uploaded from a form. So here's some simple resize code:

$imgrel = '../images/upload/'.basename($_FILES['pic']['name']); //gd wants relative paths
list($width,$height) = getimagesize($imgrel);
if($width<>150 || $height<>188){
$img_src = imagecreatefromjpeg($imgrel);
$img_dst = imagecreatetruecolor(150,188);
$imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, 150, 188, $width, $height);
imagejpeg($img_dst, $imgrel, 90);
}
There's the option of using imagecopyresized instead of imagecopyresampled, but I did that and it just doesn't look as good. If you don't know why, read this (I did). When I first did this, it wasn't working, but that was because I forgot the imagejpeg(). Oops. By the way, the 90 in that means 90% quality for that jpeg.

So now I suppose you want to resize your images keeping the aspect ratio. Well, I'm just going to share some code I found that seems to work for me. This one is from 9tutorials:
function resizeImage($originalImage, $toWidth, $toHeight){

list($width, $height) = getimagesize($originalImage);
$xscale = $width/$toWidth;
$yscale = $height/$toHeight;

//recalculate new size with default ratio
if($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}else{
$new_width = round($width * (1/xscale));
$new_height = round($height * 1/xscale));
}
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}

There we go. Easy as that. If you don't like that one, there's a different one at Netlobo which is the same as one on Travidal

Now just remember when you're doing this that GD2 is pretty process intensive so if you're resizing lots of images or large images, it's probably better to go with something like imagemagick.

Friday, May 18

Engineers vs. IT

I work at a university for a subset of Civil Engineering: Traffic Engineering. I'm just a lowly tech person, so I make websites and maintain computers and take orders from PhD's and Engineers. In case I haven't made it clear enough, to engineers it seems anyone who isn't an engineer is lower class citizen. And pretty stupid at that. I really have a problem with this. Not because I think I'm smarter than them at traffic engineering, but because I KNOW I'm smarter than them at technical stuff.

Now traffic engineering has been stepping into the world of technology with Intelligent Transportation Systems (ITS) and these poor engineers don't really have any idea. They seem to be taking baby steps and I think it's because of their opinion toward IT people, or non-engineers. For example, I met the technical support manager for Econolite, a major manufacturer of ITS equipment. He was really excited because their new software (version 9, just released this May) has IP support. "This means you can configure the cameras from anywhere!" he excitedly tells me. Yeah! That's the point of the internet! Up until then, you actually have had to go to the camera and connect to it with a laptop and a serial cable.

Now obviously this is a great idea, but it was still implemented pretty poorly. The webapp was clunky and written in Java. (The cool thing is since he's pretty high up, he's got some powers. I asked him if they developed in house and when he said yes, I suggested AJAX. 5 minutes later he tells me he sent an email to the developers suggesting it.) So what I'm wondering is, how much sooner would they have had IP connections to their cameras if it were IT people working on this instead of engineers?

Another example: I was reading about some plans for a particular city for phasing in their cameras. They have 300-ish cameras set up and only 20 some are being used. Their problem is they are using dial up to access the cameras. They want to set up a wireless mesh network between the lights for connectivity instead. Very cool idea. The problem they're running into is the project has come in way over budget for them. Now, this university I work for has another project from a different college (not engineering, but IT) which is trying to supply wireless networks around the city. Hmm... seems like a match made in heaven doesn't it?? Well, the engineering PhD's here didn't seem to thing so and threw a royal fit when that college tried to pair up with the city.

How much farther would we be if we could all just get along? How many other groups are separated by stupid ego issues and could have advanced so much farther if they just worked together?

Tuesday, May 15

PEAR Mail for Contact Info Check

I was asked to make an email that would send out every 6 months to see if the user's contact information had changed. So I needed to send an email to everyone in our database comprised of their information. I already mentioned using scheduled tasks, but we've got to make the file for it to run first. I thought it was going to be complicated, but it turned out to be quite easy. All I had to do was put the entire email into a while loop that went through the database. This is where the PEAR mail class definitely outshines PHP mail() because you can set a persistent connection to the server until you're done sending emails.

include 'Mail.php';

$get_contact = mysql_query("SELECT * FROM contact_info");
while($row = mysql_fetch_assoc($get_contact)){
$recipients = $row['email'];

$params['host'] = 'smtp.server.com';
$params['persist'] = true;

$headers['To'] = $row['FirstName'].' '.$row['LastName'].' <'.$row['email'].'>';
$headers['From'] = 'Me <mail@mail.com>';
$headers['Subject'] = 'Please Check Contact Information';

$body = 'Dear '.$row['FirstName'].' '.$row['LastName'].',

Some stuff about you should check your contact info. Here's your contact info';

//then just list their info in the body or add more to the body with .=
//then just mail it like before

$mail_object =& Mail::factory('smtp', $params);
$mail_object->send($recipients, $headers, $body);

} //don't forget you're in a while loop

Of course, if someone's gotten a new email and hasn't updated your list, you're still screwed, but oh well! What can ya do?

PEAR Templates

I recently discovered the place I work for has made their website templates as a PEAR extension. Very cool. I've been in the process of converting my pages. It's nice to work with because, for example, the standard no content template is a whopping 105 lines long. That's really long! The PEAR templates can do the same thing in 6 lines. I've reduced some of my pages from over 300 lines to less than 100. They still look the same and there's all those lines if you view the source on the page, but it's much much easier to work with this way. There's still some bugs, but they're working on that (hopefully).

So, I know this helps no one out there, it's just opinion. I would like to say that I've found the PEAR modules I've used mostly to be helpful (once I figured out how to use them).

Monday, May 7

lols for typos

My friend just showed me this and it was so funny I just had to share.

running Debian/Ubuntu box, type:

sudo apt-get install sl

after it finishes, run sl

read the man page for it for even more silliness

!!!

When is Date() a string?

Simple example says it all:


<?php
include 'Date.php';
$date = new Date();
$d = $date->format('%Y-%m-%D');

if(is_string($date)){
echo "date is a string<br />";
}else echo "date is not a string<br />";

if(is_string($d)){
echo "d is a string<br />";
}else echo "d is not a string<br />";
?>

returns:

date is not a string
d is a string

Friday, May 4

More PEAR Mail Tidbits

Yep, still playing with it. I'm sending emails to people who've decided they want to receive updates from the organization. I did like:

$query = mysql_query("SELECT email FROM info WHERE rcv_updates = 'yes'");
while($row = mysql_fetch_assoc($query)){
$headers['bcc'] .= $row['email'].",";
$recipients .= $row['email'].",";
}
Then all the regular stuff. It was kind of a shot in the dark for me, Not quite sure if it would work. Well, it did. The cool thing was since I didn't specify a To and only bcc's, the To field in the email I got was "undisclosed-recipients:;"

neat

Wednesday, May 2

Scheduled Tasks FTW

So easy thing today. I'd never done it before so it feels like an accomplishment. I need to send automatic emails every 6mo or so... enter Scheduled Tasks. So I created my script, just a php file that would send a mail when ran. Opened scheduled tasks... made the mistake of trying to use the wizard. Bad idea. That wizard is for n00bs so I couldn't figure it out (ha). Here's my steps:

  1. Right click -> new-> task
  2. Name it
  3. Double click it
  4. in run: type -- C:\php5\php.exe "C:\php5\includes\schedtest.php"
  5. set schedule in Schedule tab
  6. set password
That was it. Seriously. That easy. I couldn't believe it, but it actually worked.

Tuesday, May 1

PEAR Date compare()

Alright. I've got this figured out. I'm not sure if this is exactly how you're supposed to do stuff, being new to PEAR as I am, but this seems to work.

The problem:
I have due dates stored in a database. On the webpage, I want to say the due date, then say how many days until the due date (or how many days have passed since the due date.

The code:

We start of creating two date objects, one from the database, one for today.

$due = new Date($row['due_date']);
$d = new Date();
so now we want to compare them. PEAR has a cute little compare function for us. It returns 0 if the dates are equal, -1 if date1 is before date2 and 1 if date1 is after date2.
$comp = Date::compare($due, $d);
So now, we'll get 0, -1 or 1 and we need to handle them. And I want to find out how many days till the project is due.
if($comp == '1'){
$x = Date_Calc::dateToDays($due->day, $due->month, $due->year) - Date_Calc::dateToDays($d->day, $d->month, $d->year);
$daysleft = $x.' days left';
}elseif($comp == -1){
$x = Date_Calc::dateToDays($d->day, $d->month, $d->year) - Date_Calc::dateToDays($due->day, $due->month, $due->year);
$daysleft = $x.' days overdue';
}else $daysleft = 'today';
So the Date_Calc::dateToDays returns a number. It's the number of days since the Date_Calc epoch. It doesn't really matter what it is, it gives us a constant to work with. Just subtracting them returns the number of days. So my page really just says:
<strong>Date Due:</strong> <?=$due->format('%A, %B %e, %Y')?> (<?=$daysleft?>)
and displays:

Date Due: Friday, June 15, 2007 (43 days left)