我使用此代码来检测文件类型,并使用相关的图像 GD 函数来调整图像大小并将图像存储在指定的文件位置。
参数定义为:原始文件位置、文件类型和文件名。
文件类型和文件名正在从 $FILES_[][]
变量中获取信息。
但是,此代码似乎没有生成调整大小的图像,但很好地更新了数据库中的新文件名列。
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;
}
最佳答案
您设置新变量:
$newwidth = $w;
$newheight = $h;
使用未在任何地方定义的 $w
和 $h
。您可能想要使用通过 getimagesize
获得的 $width
和 $height
变量。
关于mysql - 使用 PHP GD 调整图像大小并保存图像。这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25060517/