Jump to content

Need a simple php image resize script


Redlightpacket

Recommended Posts

I don't think you are supposed to make requests here, but I happen to have a pretty simple script already. This will resize the image to the max width or height specified, maintaining the ratio:

   $size = getimagesize($filename);
   $img = imagecreatefromjpeg($filename);
   $maxWidth = 300;
   $maxHeight = 300;
   if ($size[0] > $size[1] && $size[0] > 300)
   {
      $newHeight = $size[1]/($size[0]/$maxWidth);
      $newWidth = $maxWidth;
   }
   else if ($size[1] > $size[0] && $size[1] > 300)
   {
      $newHeight = $maxHeight;
      $newWidth = $size[0]/($size[1]/$maxHeight);
   }
   else
   {
      $newHeight = $size[1];
      $newWidth = $size[0];
   }
   $tn = imagecreatetruecolor($newWidth, $newHeight);
   imagecopyresized($tn, $img, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.