mysql - 使用 PHP GD 调整图像大小并保存图像。这段代码有什么问题?

标签 mysql database resize save php-gd

我使用此代码来检测文件类型,并使用相关的图像 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/

相关文章:

mysql - Zend Framework 1 查询,按满足条件对结果进行排序

php - 如何将 PHPExcel 生成的文件从 PHP 页面传递到另一个 PHP 页面?

php - 扫描已使用的用户名后将数据插入mysql

mysql - grails 可以在同一个应用程序中与 MySQL 和 MongoDB 一起工作吗?

php - 在 MySql JOIN 中使用数组作为表

java - hibernate什么时候会更新相关实体?

c# - 中文本/长文本 - Entity Framework 代码优先迁移

css - 3列div css

javascript - 使用 jQuery 调整窗口大小的背景

java - 图像调整大小 - 保持纵横比