Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

November 12, 2009

Use PHP to proportionally scale an image

Filed under: PHP, The Coding World — admin @ 3:06 pm

I wrote the following function to proportionally scale images that I was pulling from a directory.

 
function img_size($img,$alt) {
 
  list($width,$height) = getimagesize($img);
 
  $maxwidth = 140; //set max width here
 
  if ($width > $maxwidth) {
	$i = $width - $maxwidth;
	$x = $width - $i;
	$percent = $maxwidth/$width;
  }
  else
  {
    $x = $width;
    $percent = 1;
  }
 
  $y = $percent * $height;
 
  $img_src = '<img src="'.$img.'" width="'.$x.'" height="'.$y.'" alt="'.$alt.'" border="0" />';
 
  return $img_src;
}
 
echo img_size('http://somesite.com/image.jpg', 'some alt text'); 
//result <img src="http://somesite.com/image.jpg" width="140" height="(proportionate to 140)" alt="(alt)" border="0">

Leave a Reply

© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.