首頁 >後端開發 >php教程 >PHP GD 库无法处理 post 上传的图片

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

WBOY
WBOY原創
2016-06-06 20:28:481069瀏覽

代码如图:

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

运行环境:

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

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

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

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

  • <?php 前没有内容

尝试过这些:

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

<code><?php $target=imagecreatefrompng($_FILES['test']['tmp_name']);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);</code></code>
  • 读取服务端文件并输出,成功

<code><?php $target=imagecreatefrompng('./interface/render_pic_v1/templates/bitcard/front.png');
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);</code></code>
  • 重命名上传的文件并输出,失败

<code><?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);</code></code>
  • 读取上次会话上传的文件并输出,成功

<code><?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);</code></code>

测试工具是 Chrome 的 Postman:

  • 成功截图:

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

-失败截图:

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

折腾过的全部代码:

<code><?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);</code>

回复内容:

代码如图:

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

运行环境:

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

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

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

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

  • <?php 前没有内容

尝试过这些:

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

<code><?php $target=imagecreatefrompng($_FILES['test']['tmp_name']);
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);</code></code>
  • 读取服务端文件并输出,成功

<code><?php $target=imagecreatefrompng('./interface/render_pic_v1/templates/bitcard/front.png');
    header("Content-type: image/png;");
    imagepng($target); 
    imagedestroy($target);</code></code>
  • 重命名上传的文件并输出,失败

<code><?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);</code></code>
  • 读取上次会话上传的文件并输出,成功

<code><?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);</code></code>

测试工具是 Chrome 的 Postman:

  • 成功截图:

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

-失败截图:

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

折腾过的全部代码:

<code><?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);</code>

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn