0

I've used this code to detect the file type and use relevant image GD functions to resize and store images in the specified file location.

Arguments are defined as: the original file location, file type and file name.

File type and file name are fetching information from the $FILES_[][] variable.

However, this code doesn't seem to generate the resized image, but very well updates the new filename column in the database.

function resize_image($file, $type, $name) {
list($width, $height) = getimagesize($file);
$newname = 'small'.$name;
  $newwidth = $w;
  $newheight = $h;

if($type == 'image/jpeg' || 'image/jpg')
{

$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($dst, $newname);
}
else if($type == 'image/gif')
{

 $src = imagecreatefromgif($file);
  $dst = imagecreatetruecolor($newwidth, $newheight);
 imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 imagegif($dst, $newname);
 }
 else if($type == 'image/png' || 'image/x-png')
{

$src = imagecreatefrompng($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($dst, $newname);
 }

else
 {
 die("Invalid file! Please try again!");
}

return $newname;
 }
Varun Nath
  • 5,570
  • 3
  • 23
  • 39
Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38

1 Answers1

0

You set new variables:

$newwidth = $w;
$newheight = $h;

using $w and $h which are not defined anywhere. You probably want to use $width and $height variables which you get by using getimagesize.

mateuszk
  • 35
  • 4