Discover & Rate New Music Check out ChartVote. Promote the music you like.

Archive for December, 2006

PHP Crop Image

This function will crop an image to the specified width and height without distorting it and in most cases without adding black regions to the image. This function is great for profile photos where the image needs to fit a certain area, but the images being uploaded are of all sizes.

Example
cropexample.jpg

Code

PHP:
  1. cropImage(225, 165, '/path/to/source/image.jpg', 'jpg', '/path/to/dest/image.jpg');
  2.  
  3. function cropImage($nw, $nh, $source, $stype, $dest) {
  4.  
  5.     $size = getimagesize($source);
  6.     $w = $size[0];
  7.     $h = $size[1];
  8.  
  9.     switch($stype) {
  10.         case 'gif':
  11.         $simg = imagecreatefromgif($source);
  12.         break;
  13.         case 'jpg':
  14.         $simg = imagecreatefromjpeg($source);
  15.         break;
  16.         case 'png':
  17.         $simg = imagecreatefrompng($source);
  18.         break;
  19.     }
  20.  
  21.     $dimg = imagecreatetruecolor($nw, $nh);
  22.  
  23.     $wm = $w/$nw;
  24.     $hm = $h/$nh;
  25.  
  26.     $h_height = $nh/2;
  27.     $w_height = $nw/2;
  28.  
  29.     if($w> $h) {
  30.  
  31.         $adjusted_width = $w / $hm;
  32.         $half_width = $adjusted_width / 2;
  33.         $int_width = $half_width - $w_height;
  34.  
  35.         imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
  36.  
  37.     } elseif(($w <$h) || ($w == $h)) {
  38.  
  39.         $adjusted_height = $h / $wm;
  40.         $half_height = $adjusted_height / 2;
  41.         $int_height = $half_height - $h_height;
  42.  
  43.         imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
  44.  
  45.     } else {
  46.         imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
  47.     }
  48.  
  49.     imagejpeg($dimg,$dest,100);
  50. }

Today is a Good Day

I love Akismet...Neh! I REALLY love Akismet.

CSS Development Techniques

CSS has always been my least favorite part of developing a site. When I am writing a site from the ground up, I will make my layouts using inline styles to speed up prototyping and once I have finalized on my layout I will then go back through my design and write the CSS for it.

I am curious how other developers/designers out there go about their CSS. Leave a comment and let me know. I am open for suggestions on my method.

RSS Feed



Recommended Sites