I had a project that required images to be automatically cropped and resized to the exact same dimensions regardless of the original file. Automatically resizing proportionally was easy, automatically cropping to a percentile was easy, but being able to specify the dimensions and have it automatically resize and crop to fit from the center if needed, is a bit complicated!

Here’s the function. $srcimg is an image reference, not an actual file.


function Thumbnail($srcimg,$tn_w,$tn_h) {
$src_w = imagesx($srcimg);
$src_h = imagesy($srcimg);
$src_ratio = $src_w/$src_h;
if ($tn_w/$tn_h > $src_ratio) {
$new_h = $tn_w/$src_ratio;
$new_w = $tn_w;
} else {
$new_w = $tn_h*$src_ratio;
$new_h = $tn_h;
}
$x_mid = $new_w/2;
$y_mid = $new_h/2;
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $srcimg, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($tn_w/2)), ($y_mid-($tn_h/2)), $tn_w, $tn_h, $tn_w, $tn_h);
imagedestroy($newpic);
imagedestroy($srcimg);
return $final;
}

Tall images will be cropped from the center so the bottom and top are cut off. Wide images will be cropped from the center so the sides are cut off. Proportional images will remain uncropped and only resized to the dimensions you pass to this function. Here’s what it might look like in action.

The form:


<form enctype="multipart/form-data" method="post">
<input name="pic" type="file" />
<input type="submit" value="Crop and resize" />
</form>

The processing:


$img = imagecreatefromjpeg($_FILES['pic']['tmp_name']); //assuming it's a jpeg
$width = 410;
$height = 396;
$path = '/this/is/where/my/file/goes/'.$_FILES['pic']['name'];
$newimg = Thumbnail($img,$width,$height);
Imagejpeg($newimg,$path,80) //again, assuming jpeg, 80% quality

The new thumbnailed image will be placed in /this/is/where/my/file/goes with the same name as the original file. Hooray!

Initial image:
70-the-most-awesome-picture-ever-taken

Cropped/resized:
38-19-0-big

Share/Save/Bookmark

My buddy from Top SEO Consultants asked me to whip up a wordpress plugin to throw tweetmeme’s “retweet this” button into posts with options to position the button.

It’s in Dev Projects.

I just noticed that spell-check is flagging the terms “SEO”, “WordPress”, and “plugin”. You’d think they’d be commonplace enough by now that they’d be included in those dictionaries by default.

Anyway, the plugin is here: Tweetmeme Retweet Button.

Share/Save/Bookmark

Want to share images on twitter with your own host? There’s already a lot of services out there for hosting your images, and there are some services that will automatically send a tweet for your uploaded image via twitter as well. But I have my own domain, my own hosting, and I’d like to keep my images here.

Today was a very lazy sunday, but I managed to get a little something accomplished. Check out the Development Projects section and download my single-file script that lets you upload an image and automatically send a tweet to it, or just Download Here. It’s very basic, and now that I think about it, really open to abuse, because anyone with a valid twitter username and password can use it. I guess if anyone actually uses it, or I get bored enough again, I’ll add some more features like URL-shortening services or more security. Right now, the length of your message is limited by the length of your domain name and the directory the script resides in.

You can see (and use, I’ll leave it up until it starts getting abused) a live demo here: Image Upload and Tweet. Let me know if you find any bugs, please!

Share/Save/Bookmark

Page 1 of 212»

ismoketoomuch turtle dance ismoketoomuch turtle drums ismoketoomuch turtle guitar