【问题标题】:Resizing image using PHP GD and saving the image. What's wrong with this code?使用 PHP GD 调整图像大小并保存图像。这段代码有什么问题?
【发布时间】:2014-07-31 13:46:32
【问题描述】:

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

【问题讨论】:

    标签: mysql database resize save php-gd


    【解决方案1】:

    您设置了新变量:

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

    使用未在任何地方定义的 $w$h。您可能想要使用通过使用getimagesize 获得的$width$height 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2012-08-12
      • 2019-03-17
      • 2020-01-02
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多