PHP GD 库无法处理 post 上传的图片


代码如图:


 <?php
    $target=imagecreatefrompng($_FILES['test']['tmp_name']);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);

运行环境:

Win64-apache2.4.9-php5.5.12
Ubuntu64-apache2.4.7-php5.5.9

我百度/Google 查了很多,可以确定:

  • GD库已经开启(成功处理验证码并输出)

  • 文件夹权限正常(读写文件成功)

  • <?php 前没有内容

尝试过这些:

  • 直接输出上传的文件,失败


 <?php
    $target=imagecreatefrompng($_FILES['test']['tmp_name']);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);

  • 读取服务端文件并输出,成功


 <?php
    $target=imagecreatefrompng('./interface/render_pic_v1/templates/bitcard/front.png');
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);

  • 重命名上传的文件并输出,失败


 <?php
    $target_path=$_FILES['test']['tmp_name'].'.png';
    move_uploaded_file($_FILES['test']['tmp_name'],$target_path);
    $target=imagecreatefrompng($target_path);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);

  • 读取上次会话上传的文件并输出,成功


 <?php
//    $target_path=$_FILES['test']['tmp_name'].'.png';
    $target_path="C:\\Program Files (x42)\\WampServer\\tmp\\phpB808.tmp.png";
//    move_uploaded_file($_FILES['test']['tmp_name'],$target_path);
    $target=imagecreatefrompng($target_path);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);

测试工具是 Chrome 的 Postman:

  • 成功截图:

clipboard.png

-失败截图:

clipboard.png

折腾过的全部代码:


 <?php
//    header("Content-type: text/html; charset=utf-8");
//    $x=$interface_path.'/templates/bitcard/front.png';
//    $interface_path='./interface/render_pic_v1';
//    $x='./interface/render_pic_v1/templates/bitcard/front.png';
//    $target=imagecreatefrompng($x);
//    require $interface_path.'/functions_lib/image_process.php';

//    copy($_FILES['test']['tmp_name'],'./interface/render_pic_v1/result/test.png');
//    rename($_FILES['test']['tmp_name'],$_FILES['test']['tmp_name'].'.png');
//    $target_path='./interface/render_pic_v1/result/'.md5_file($_FILES['test']['tmp_name']).'-'.time().'-'.$_FILES['test']['name'];
//    move_uploaded_file($_FILES['test']['tmp_name'],$target_path);
//    $target=imagecreatefrompng($target_path);
//    $target=imagecreatefrompng($_FILES['test']['tmp_name'].'.png');
//    $target=imagecreatefrompng($_FILES['test']['tmp_name']);
//    if($target=fopen($_FILES['test']['tmp_name'])){echo "string2";}
//    $source=imagecreatefrompng('C:\\Program Files (x42)\\WampServer\\tmp\\phpE925.tmp.png');
//    $source=imagecreatefrompng('/var/www/html/bit_if/temp/phpniambi.png');
//    $target=imagecreatefrompng('./interface/render_pic_v1/result/test.png');
//    $target=imagecreatefrompng('./interface/render_pic_v1/result/test');
//    var_dump($_FILES['test']['tmp_name']);
//    var_dump($_FILES['test']['tmp_name'].'.png');
//    $position_target=array('x'=>330,'y'=>339);
//    $position_source=array('x'=>0,'y'=>0,'w'=>288,'h'=>402);
//    setPicture($target, $source, $position_target, $position_source);

//    header("Content-type: image/png; charset=utf-8");
//    header("Content-type: image/png;");
//    imagepng($target); 
//    imagedestroy($target);

//    $target=imagecreatefrompng($_FILES['test']['tmp_name']);
//    var_dump($_FILES);

post php gd

河蟹爆发者 9 years, 3 months ago

问题基本就是$_FILES的路径和绝对路径有区别,建议你可以把$_files打印出来看看

拆迁办事处 answered 9 years, 3 months ago

Your Answer