PHP Image upload to folder and making Thumbnail Images

Hello, I have this code to get one image in to image folder and one image in to image/thumbs folder. But the problem is that the image that ends up in the thumbs folder is just as big in pixels as the main image. So what is going on here. Thank you.

<?php
ready = "OK, Uppladdat";
uploaddir = "images/";
uploaddir_th = "images/thumbs/";
allowed = array('jpg','jpeg','gif','png');
max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size) 
{
$file_ext  = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed)) 
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name)) 
{
   $name = $file_name.'['.$x.'].'.$file_ext;
   $x++;
}
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
   chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
   die("Invalid file type");
}
}
else
{
  die("File size too big");
}

//thumbnail image making part

list($width, $height) = getimagesize($uploaddir.'/'.$name);
$modwidth = 200;
$modheight = 140;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploaddir.'/'.$name);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight);
imagejpeg($image, $uploaddir_th.'/'.$name);
}
echo $ready; 
?>

imagecopyresampled expects two more parameters

http://php.net/manual/en/function.imagecopyresampled.php

2 Likes

Thank you, i got it now:

//thumbnail image making part

		
          $modwidth = 200;

          $modheight = 140;

          list($width, $height) = getimagesize($uploaddir.'/'.$name);
		  
		  $ratio_orig = $width/$height;
		  
		  if ($width/$height > $ratio_orig) {
		  $width = $height*$ratio_orig;
		  } else {
		  $height = $width/$ratio_orig;
	      }

          $tn = imagecreatetruecolor($modwidth, $modheight);

          $image = imagecreatefromjpeg($uploaddir.'/'.$name);

          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

          imagejpeg($tn, $uploaddir_th.'/'.$name);
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.