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 – Constrained Image Resize Min/Max Values

Constrained Image Resize Min/Max Values

This script is awesome for say resizing an image so it fits in a certain width without losing it’s aspect ratio. Also good for crop tools.

Snippet:

  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&#039;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/constrained-image-resize-minmax-values/
  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&#039;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/constrained-image-resize-minmax-values/
  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 (MAXIMUM):

  1. <?
  2.  // Constrained Image Resize MAXIMUM Example
  3.  // load the image
  4.  $im = imagecreatefromjpeg("test.jpg");
  5.  
  6.  // resize the image (to be NO MORE then 200 pixels wide or 500 pixels high)
  7.  $im = c_resize( $img, 200, 500);
  8.  
  9.  // display the image
  10.  imagejpeg($im);
  11. ?>

Example (MINIMUM):

  1. <?
  2.  // Constrained Image Resize MINIMUM Example
  3.  // load the image
  4.  $im = imagecreatefromjpeg("test.jpg");
  5.  
  6.  // resize the image (to be NO LESS then 200 pixels wide or 500 pixels high)
  7.  $im = c_minresize( $img, 200, 500);
  8.  
  9.  // display the image
  10.  imagejpeg($im);
  11. ?>
GD Star Rating
loading...

Originally posted 2009-10-17 00:26:01.

Popularity: 12%

Posted by irbobo   @   15 August 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
« Get Google Page Rank of a Site
Next Post
Snoopy PHP Net Client »
Powered by Wordpress   |   Lunated designed by ZenVerse