使用php上传带有调整大小错误的照片

时间:2017-11-02 04:06:58

标签: php

我在此代码中有错误:

  copy($newFile,$target_dir.'slider_'.$newName);
  $file2 = $target_dir.'slider_'.$newName; //This is the original file
  list($width2, $height2) = $newFile;

  $modwidth2 = 750;
  $modheight2 = 300;
  $tn2 = imagecreatetruecolor($modwidth2, $modheight2);

  if ($imageFileType == "jpg"){

    $image2 = imagecreatefromjpeg($newFile);
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2);
    imagejpeg($tn2, $file2, 50);

  } elseif  ($imageFileType == "png"){

    imagealphablending($tn2, false);
    imagesavealpha($tn2, true);
    $image2 = imagecreatefrompng($newFile);
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2);
    imagepng($tn2, $file2, 9);

  } elseif  ($imageFileType == "jpeg"){

    $image2 = imagecreatefromjpeg($newFile);
    imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2);
    imagejpeg($tn2, $file2, 9);

  }

如果我上传照片,输出始终为黑色。你能告诉我这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

你没有得到宽度&身高正确。

list($width2, $height2) = $newFile;

更改为

list($width2, $height2) = getimagesize ($newFile)