Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Zoom Crop Image

Zoom Crop Image

This snippet is great for creating thumbnails of varying dimensions. For example if you wanted to create a rectangular thumb automatically. Basically you load an image (ie. $im = imageloadfromjpeg) into a variable and then you can pass that variable to this snippet and have it return a new image variable.

Snippet:

  1. <?
  2.  // gives a nice rectangular crop of an image
  3.  // this function depends on the constrained MINIMUM function…
  4.  // zoom crops are useful for making rectangular thumbs instantly
  5.  // of images…
  6.  // http://thedevnet.com/php/snippets/zoom-crop-image/
  7.  function zoom_crop($img, $w, $h)
  8.  {
  9.   // min resize the bastard…
  10.   $img = c_minresize($img, $w, $h);
  11.  
  12.   $orig_w = imagesx($img);
  13.   $orig_h = imagesy($img);
  14.  
  15.   // set where the crop should begin width and height wise..
  16.   // /2 will be center… however.. most portrait images have the
  17.   // content action near the top.. so that's why I use /5 for
  18.   // vertically cropped images… adjust to fit your need..
  19.   $start_y = floor(($orig_h - $h) /5);
  20.   $start_x = floor(($orig_w - $w) /2);
  21.  
  22.   // now lets create a new image…
  23.   $new_im = imagecreatetruecolor($w, $h);
  24.  
  25.   imagecopy($new_im, $img, 0,0, $start_x, $start_y, $w,$h);
  26.  
  27.   return $new_im; // return the new image…
  28.  }
  29. ?>

Snippet 2: (required from Constrained Image Resize Min/Max )

  1. <?
  2.  // constrained resize.. (max value)
  3.  // constraining to a MAXIMUM value means that we want the image
  4.  // to be NO MORE then $w or $h…
  5.  // so when it resizes an image… it'll constrain it to the same dimensions
  6.  // but it will not exceed in width or height the values of $max_width or $max_height
  7.  // http://thedevnet.com/php/snippets/zoom-crop-image/
  8.  
  9.  function c_resize( $img, $max_width, $max_height)
  10.  {
  11.   $FullImage_width = imagesx ($img); // Original image width
  12.   $FullImage_height = imagesy ($img); // Original image height
  13.  
  14.   // now we check for over-sized images and pare them down
  15.   // to the dimensions we need for display purposes
  16.   $ratio = ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
  17.   $new_width = ((int)($FullImage_width * $ratio));
  18.   $new_height = ((int)($FullImage_height * $ratio));
  19.   //check for images that are still too high
  20.   $ratio = ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
  21.   $new_width = ((int)($new_width * $ratio)); //mid-size width
  22.   $new_height = ((int)($new_height * $ratio)); //mid-size height
  23.  
  24.   $NEW_IM = imagecreatetruecolor( $new_width , $new_height );
  25.   imagecopyresampled( $NEW_IM, $img,
  26.   0,0, 0,0, //starting points
  27.   $new_width, $new_height,
  28.   $FullImage_width, $FullImage_height );
  29.  
  30.   return $NEW_IM;
  31.  }
  32.  
  33.  // constrained resize.. (min value)
  34.  // constraining to a minimum value means that we want the image
  35.  // to be NO LESS then $w or $h…
  36.  // so when it resizes an image… it'll constrain it to the same dimensions
  37.  // but it will not be reduced less then a certain width or certain height…
  38.  // this is useful for a crop function
  39.  // http://thedevnet.com/php/snippets/zoom-crop-image/
  40.  function c_minresize($img, $w, $h)
  41.  {
  42.   $src_w = imagesx($img);
  43.   $src_h = imagesy($img);
  44.  
  45.   $pct_w = ($w / $src_w) * 100;
  46.   $pct_h = ($h / $src_h) * 100;
  47.  
  48.   if($pct_w > $pct_h)
  49.   {
  50.    $new_w = $src_w * ($pct_w / 100);
  51.    $new_h = $src_h * ($pct_w / 100);
  52.   }
  53.   else
  54.   {
  55.    $new_w = $src_w * ($pct_h / 100);
  56.    $new_h = $src_h * ($pct_h / 100);
  57.   }
  58.  
  59.   $new_w *= 1.05;
  60.   $new_h *= 1.05;
  61.  
  62.   return c_resize( $img, $new_w, $new_h);
  63.  }
  64. ?>

Example:

  1. <?
  2.  // load the image
  3.  $im = imagecreatefromjpeg("test.jpg");
  4.  // crop the image (make it 200 pixels wide and 100 pixels high)
  5.  $im = zoom_crop($img, 200, 100);
  6.  // display the image
  7.  imagejpeg($im);
  8. ?>
GD Star Rating
loading...

Originally posted 2009-10-16 23:05:06.

Popularity: 40%

Posted by irbobo   @   17 July 2010
Tags : , , ,

Related Posts

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« Display an image or URL on a specific date
Next Post
Simple PHP Caching Snippet »
Powered by Wordpress   |   Lunated designed by ZenVerse