Page 1 of 1

changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:22 pm
by Carbine
I just quickly made this code, it resizes images. You just put the image url, max height and max width. It takes a little while to do it though :( and I don't know if there is any errors yet, only just made it and tested it and seems fine. It keeps it in the same aspect ratio, just resizes it. If there is errors, tell me else it's going into my libary, core thing.
Version 1
function changeSize($image, $height, $width)
{
  @$type = end(explode(".", $image));
  if ($type=="jpg"||$type=="jpeg"||$type=="png"||$type=="gif"||$type=="bmp")
  {
    if ($height>$width) $max = $width;
    else $max = $height;
    list($iheight, $iwidth) = getimagesize($image);
    if ($iheight>$iwidth) $h = $iheight;
    else $h = $iwidth;
    $p = $h/$max;
    if ($h==$iheight)
    {
      $nheight = $max;
      $nwidth = round(($iheight/$iwidth)*$max);
    }
    else
    {
      $nwidth = $max;
      $nheight = round(($iwidth/$iheight)*$max);
    }
    echo "<img src='$image' width='$nwidth' height='$nheight' />";
  }
  else{
    echo "The image is not supported!";
  }
}
Version 2
function changeSize($image, $height, $width)
{
  $type = substr($image, strrpos($image, ".") +1);
  if ($type=="jpg"||$type=="jpeg"||$type=="png"||$type=="gif"||$type=="bmp")
  {
    if ($height>$width) $max = $width;
    else $max = $height;
    list($iwidth, $iheight) = getimagesize($image);
    if ($iheight<=500 && $iwidth<=500)
    {
      if ($iheight>$iwidth) $h = $iheight;
      else $h = $iwidth;
      $p = $max/$h;
      $nheight = round($iheight*$p);
      $nwidth = round($iwidth*$p);
      echo "<img src='$image' width='$nwidth' height='$nheight' />";
    }
    else echo "File too big.";
  }
}

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:24 pm
by jacek
You could use the GD library to actually resize the image instead of outputting a resized image tag.

For what it does it looks play though.

Ideally you should avoid the use of @ to hide errors though.

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:28 pm
by Carbine
Yeah, for this time at night I can't be arsed for researching:
"Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\test\resize.php on line 4"
I think it's just the way I use to get the file extension, I guess I could use a substr() and strrpos() to get the file extension. I'll see how it works out :P
/E How do I resize an image, I really have no idea :(
/E Changed the way to getting a file extension to:
$type = substr($image, strrpos($image, ".") +1);

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:32 pm
by jacek
Carbine wrote:"Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\test\resize.php on line 4"
Those you can ignore, in your php.ini file set error_reporting to E_ALL and they will go away.
Carbine wrote:/E How do I resize an image, I really have no idea :(
There are some good examples on php.net.

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:40 pm
by Carbine
I can use the imagecopyresampled() to change the size, but if I was to use this function I made it would be for thumbnails or something. I was thinking of profile pictures being links to images and it just displays it at a different size or something. But if it's to far out then just don't allow it at all :P

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:44 pm
by jacek
Well if that's what you wan tit to do, then it looks like it will do it ;) i see what you mean, an actually resizing function has a different purpose.

The reason it seems sow was probably that php has to download the image to get its size.

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:46 pm
by Carbine
Yeah, I used a big image to test it :P If the file it too big, like as big as that image I will just not allow it at all. I'm just editing again and I'll post the new version later.

Re: changeSize(); Just something I quickly made

Posted: Fri May 06, 2011 11:47 pm
by jacek
When you use it with local files it should not be a noticeable speed drop.

Re: changeSize(); Just something I quickly made

Posted: Sat May 07, 2011 1:43 pm
by Carbine
Done version 2. Fixed a couple of errors, and changed the way the sizes are worked out to improve speed.