无法保存上传的PNG文件

时间:2012-02-01 20:39:04

标签: php upload png

我可以上传JPG文件,但不能上传PNG文件 PNG图像未保存,但输出此‰PNG À À°£ûïS JÄž Ÿ¬™Ù<íû;

我的代码是这样的:

  $source_gd_image = imagecreatefrompng( $sourcefile ); // <-- If PNG file
  $target_gd_image = imagecreatetruecolor( $target_image_width, $target_image_height );

  if($source_image_type == IMAGETYPE_PNG) {
    imagealphablending($target_gd_image, true); // Should this be false?
    imagesavealpha($target_gd_image, true); 
    imagepng($target_gd_image); // <-- This is where the gibberish is outputed
  }

  if($source_image_type == IMAGETYPE_JPEG) {
    imagecopyresampled( $target_gd_image, $source_gd_image, 0, 0, 0, 0, $target_image_width, $target_image_height, $source_image_width, $source_image_height );
    imagejpeg( $target_gd_image, $target_file, $quality );
  }

  imagedestroy( $source_gd_image );
  imagedestroy( $target_gd_image );

我做错了什么?

1 个答案:

答案 0 :(得分:4)

imagejpeg一样,如果要将其保存到文件中,则必须指定a file name as the second parameter,否则只会输出到浏览器。

假设$ target_file是所需的输出文件名,这意味着使用:

imagepng($target_gd_image, $target_file);