changeSize(); Just something I quickly made

Written something you are proud of, post it here.
Post Reply
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

changeSize(); Just something I quickly made

Post 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.";
  }
}
Last edited by Carbine on Sat May 07, 2011 1:43 pm, edited 2 times in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: changeSize(); Just something I quickly made

Post 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.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: changeSize(); Just something I quickly made

Post 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);
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: changeSize(); Just something I quickly made

Post 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.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: changeSize(); Just something I quickly made

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: changeSize(); Just something I quickly made

Post 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.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: changeSize(); Just something I quickly made

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: changeSize(); Just something I quickly made

Post by jacek »

When you use it with local files it should not be a noticeable speed drop.
Image
Carbine
Posts: 58
Joined: Fri May 06, 2011 1:47 pm
Location: UK, Nottinghamshire
Contact:

Re: changeSize(); Just something I quickly made

Post by Carbine »

Done version 2. Fixed a couple of errors, and changed the way the sizes are worked out to improve speed.
Post Reply