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;
}