我有一个用于上传 png 文件的脚本 php,但是上传的文件在圆角处会丢失一些质量(并且以原始大小的一半上传),如果我想稍后在烟花中编辑上传的 png 文件,它会显示未知图片类型或文件.
为什么上传的 png 不被 $_FILES["poza_a_1"]["type"] == "image/x-png" 识别?我被迫使用:
if($_FILES["poza_a_1"]["type"] == "image/png"){
如果我不使用它,PNG图片将不会上传。
我的代码是:
if ($_FILES["poza_a_1"]["type"] == "image/jpeg" || $_FILES["poza_a_1"]["type"] == "image/pjpeg" || $_FILES["poza_a_1"]["type"] == "image/gif" || $_FILES["poza_a_1"]["type"] == "image/x-png" || $_FILES["poza_a_1"]["type"] == "image/png" || isset($_POST["nume_a_1"])){
$current_img=$_FILES['poza_a_1']['name'];
$extension = strtolower(substr(strrchr($current_img, '.'), 1));
$new_image = str_replace(" ", "-", strtolower($e_nume));
$nume = ucwords(str_replace("-", " ", $e_nume));
$id_poza = $e_id_poza;
$originalImage = "../temps/".$new_image . "." . $extension;
$cale_poza = "../img/".$new_image."." . $extension;
$action = move_uploaded_file($_FILES["poza_a_1"]['tmp_name'], $originalImage);
//if (move_uploaded_file($_FILES['poza_a']['tmp_name'], $uploadfile_o)) {
//$max_upload_width = 1280;
//$max_upload_height = 900;
//$max_upload_width_th = 168;
//$max_upload_height_th = 130;
if($_FILES["poza_a_1"]["type"] == "image/jpeg" || $_FILES["poza_a_1"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($originalImage) ;
}
if($_FILES["poza_a_1"]["type"] == "image/gif"){
$image_source = imagecreatefromgif($originalImage);
}
if($_FILES["poza_a_1"]["type"] == "image/bmp"){
$image_source = imagecreatefromwbmp($originalImage);
}
if($_FILES["poza_a_1"]["type"] == "image/x-png"){
$image_source = imagecreatefrompng($originalImage);
}
if($_FILES["poza_a_1"]["type"] == "image/png"){
$image_source = imagecreatefrompng($originalImage);
}
imagealphablending( $image_source, true );
chmod($cale_poza,0777);
imagejpeg($image_source, $cale_poza,100);
chmod($cale_poza,0777);
//list($image_width, $image_height) = getimagesize($cale_poza);
// $new_width = $image_width;
// $new_height = $image_height;
//$new_image = imagecreatetruecolor($image_width, $image_height);
//$image_source = imagecreatefromjpeg($cale_poza);
//imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
//$new_width, $new_height,
//imagejpeg($new_image, $cale_poza, 100); // save
// imagedestroy($new_image);
$old = getcwd(); // Save the current directory
$path_to_file = '../temps/';
chdir($path_to_file);
$originalImage_del = str_replace ("../temps/", "", $originalImage);
unlink($originalImage_del);
chdir($old); // Restore the old working directory
}
好的,我使用 move_uploaded_file 解决并删除...之间的其余代码:
$originalImage = "../temps/".$new_image . "." . $extension;
$cale_poza = "../img/".$new_image."." . $extension;
//$destination = "../img/".strtolower($e_nume).'/'.$new_image."-".$y."-".$e_id_poza."_th." . $extension;
//$action = move_uploaded_file($_FILES["poza_a_1"]['tmp_name'], $originalImage);
chmod($cale_poza,0777);
move_uploaded_file($_FILES["poza_a_1"]["tmp_name"], $cale_poza);