PHP Image Scaler

By Jonesy44 on Jun 07, 2008

I've developed this snippet to scale images to a size, set by the maximum width || height.

This snippet is fantastic for galleries, cutouts, small webpages, limited sized webpage, forums, anywthing where users can manually edit the webpage and create a "possibility" of screwing it up, this should help!

This does not distort images, whatsoever! it rescales the width and height according to the maximum width/height you allocate.

I am working on developing a function which will cover both in one snippet, but for now, this is what i have :p

How it works (examples)
Insert the function in-between the img tags where width & height paramaters usually would go;

<?php

$img = 'images/myfiles/image1.jpg';
echo '<img src="' .$img. '" ' .width_scale($img,"200"). '>';

?>
///////////////////////////
// Scale images by width //
///////////////////////////
function width_scale($file,$maxwidth) {
  list($width,$height) = getimagesize($file);
  if ($width > $maxwidth) {
    $new_width = $maxwidth;
    $dec_per = $new_width / $width * 100;
    $new_height = $height / 100 * $dec_per;
    return 'width="' .$new_width. '" height="' .$new_height. '"';
  }
  else {
    return 'width="' .$width. '" height="' .$height. '"';
  }
}

////////////////////////////
// Scale images by height //
////////////////////////////
function height_scale($file,$maxheight) {
  list($width,$height) = getimagesize($file);
  if ($height > $maxheight) {
    $new_height = $maxheight;
    $dec_per = $new_height / $height * 100;
    $new_width = $width / 100 * $dec_per;
    return 'width="' .$new_width. '" height="' .$new_height. '"';
  }
  else {
    return 'width="' .$width. '" height="' .$height. '"';
  }
}

Comments

Sign in to comment.
Zmodem   -  Aug 25, 2008

Yea, that one is a lot more advanced, especially since it not only gets the job done great, but it's also more performance-optimized.

 Respond  
Hawkee   -  Aug 25, 2008

tye did a nice image scaler a while back, http://www.hawkee.com/snippet/844/

 Respond  
Zmodem   -  Aug 25, 2008

Would this work (not a big PHP user):

Syntax: image_scale($img,"200","250")

Example:

<?php

$img = 'images/myfiles/image1.jpg';
echo '<img src="' .$img. '" ' .width_scale($img,"200","250"). '>';

?>
//////////////////////////////////////////////////////////////
// Scale images by width+height //
//////////////////////////////////////////////////////////////
function image_scale($file,$maxwidth,$maxheight) {
  list($width,$height) = getimagesize($file);
  if (($width > $maxwidth) && ($height > $maxheight)) {
      $new_width = $maxwidth;
      $new_height = $maxheight;
      $dec_perw = $new_width / $width * 100;
      $dec_perh = $new_height / $height * 100;
      $new_width = $width / 100 * $dec_per;
      $new_height = $height / 100 * $dec_per;
      return 'width="' .$new_width. '" height="' .$new_height. '"';
  }
  else {
      return 'width="' .$width. '" height="' .$height. '"';
  }
}
 Respond  
Hawkee   -  Aug 25, 2008

Yeah, I would like to see a single function rather than two.

 Respond  
Jonesy44   -  Jun 16, 2008

On a large scale .. Maybe. But on a small scale, not soo bad.

Do you mean the getimagesize?

 Respond  
markusPHP   -  Jun 16, 2008

Just a note: resizing images on \'the fly\' is resource intensive.

 Respond  
Jonesy44   -  Jun 10, 2008

Oops, noticed a little error. :D

corrected $width to 100 .. i never understood percentages >.>
Kidding ^, they\'re easy .. :D

 Respond  
Jonesy44   -  Jun 08, 2008

Hehe, yeesh. problem sorted now anyways lol

 Respond  
F*U*R*B*Y*   -  Jun 08, 2008

Sure Ballopah, you have me on MSN, so might as well use that ;)

 Respond  
Jonesy44   -  Jun 08, 2008

lol, no worries.

Furbs, i was wondering if i can catch u on msn/irc sometime, i need a bit of help with my gallery lol

 Respond  
F*U*R*B*Y*   -  Jun 08, 2008

rofl, nice, haven\'t been able to try it as i\'m doing school work atm. But it looks nice. :) good job

 Respond  
Jonesy44   -  Jun 08, 2008

someone go learn PHP and comment ! :P

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.