Top

PHP Image Scaler


PHP Code
+ 0 likes
Please Register to submit score.
Average Score  4.0
Scores Submitted  2
Date Added  Jun 07, 2008
Last Updated  Jun 10, 2008

Introduction

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;

Code:
<?php

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

?>


Grab the Code

Comments

  (12)  RSS
jonesy44
Comments: 907
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 8, 2008 4:01 am
someone go learn PHP and comment ! :P
F*U*R*B*Y*
Comments: 551
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 8, 2008 5:24 am
rofl, nice, haven\'t been able to try it as i\'m doing school work atm. But it looks nice. :) good job
jonesy44
Comments: 907
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 8, 2008 7:20 am
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
F*U*R*B*Y*
Comments: 551
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 8, 2008 8:54 am
Sure Ballopah, you have me on MSN, so might as well use that ;)
jonesy44
Comments: 907
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 8, 2008 9:37 am
Hehe, yeesh. problem sorted now anyways lol
jonesy44
Comments: 907
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 10, 2008 11:16 am
Oops, noticed a little error. :D

corrected $width to 100 .. i never understood percentages >.>
Kidding ^, they\'re easy .. :D
markusPHP
Comments: 10
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 16, 2008 11:39 am
Just a note: resizing images on \'the fly\' is resource intensive.
jonesy44
Comments: 907
 
PHP Snippet:  PHP Image Scaler
Posted on Jun 16, 2008 11:52 am
On a large scale .. Maybe. But on a small scale, not soo bad.

Do you mean the getimagesize?
Hawkee
Comments: 479
 
PHP Snippet:  PHP Image Scaler
Posted on Aug 25, 2008 11:07 pm
Yeah, I would like to see a single function rather than two.
Zmodem
Comments: 119
 
PHP Snippet:  PHP Image Scaler
Posted on Aug 25, 2008 11:21 pm
Would this work (not a big PHP user):

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

Example:
Code:
<?php

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

?>


Code:
//////////////////////////////////////////////////////////////
// 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. '"';
  }
}
Hawkee
Comments: 479
 
PHP Snippet:  PHP Image Scaler
Posted on Aug 25, 2008 11:26 pm
tye did a nice image scaler a while back, http://www.hawkee.com/snippet/844/
Zmodem
Comments: 119
 
PHP Snippet:  PHP Image Scaler
Posted on Aug 25, 2008 11:27 pm
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.

Please Register or Login to start posting comments.
Bottom