PHP图片上传失败了。已上传半张图片

时间:2015-02-12 03:57:46

标签: java php android image yii

如何检测图像是完全上传还是部分上传?我遇到了一个奇怪的情况,只上传了一半的图像。用户通过Web服务上传了以下图像:

enter image description here

管理门户和Web服务是使用Yii框架编写的。可能导致图像像这样创建的原因是什么? Android Developer发送base-64编码的字符串,我按以下方式对其进行解码:

$profilePicture = $data['profilePicture'];
    header('Content-Type: image/png');
    $rnd = sprintf('%04d', rand(0, 9999));
    $fileName = $rnd . '_' . time() . '.png'; // random number + timestamp

    $uploadPath = User::getProfilePicturePath() . $id;

    if (!file_exists($uploadPath) and ! is_dir($uploadPath)) //create a directory only if it does not exist.
        mkdir($uploadPath);

    if (!file_put_contents($uploadPath . DIRECTORY_SEPARATOR . $fileName, base64_decode($profilePicture)))

我怀疑这是原因,因为Android Developer在文件完全上传之前发送了base-64编码。还是别的什么?

$ profilePicture等于64基本字符串,

iVBORw0KGgoAAAANSUhEUgAAAS0AAAEsCAIAAAAZ3XIcAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABSSSURBVHhe7dzLjixYWp7hFAOPPPDM9+FLsGxsbGTESYBADH2mD3RXd1UfweC78z3hqtAjhbResUVZtTtiyd+jb5CpNwehvfJX7yoVfPzgP39vu3o//C/f327fx4/+6w+2bXvtdofb9vrtDrft9dsdXr8f/7cfbrdvd3j9jhfdbtzu8PodL7rduI/j++26ffHf/3q7fbvD63e86HbjdofX73jR7cZ9HN9v1+0n/+NH2+3bv6e5fseLbjfu4/h+27bf/Pb30ut3vOh243aH1+940e3G7Q6v3/Gi243bHV6/40W3G7d/T7Ntr9/ucNtev93htr1+u8Ptc+2n//PH2z9xu8Ptc+34Vds+sd3h9rl2/Kptn9jH8f22fVf76ns/ebf97Ps/fc99fPlXX2zb59hxA9sn9nF8v23f1Y6z3D6xN/176fEp32TH79mb7Pgbznbj3vTvpcev2pvs+LPbtu9q+3vpt9jxZ7dt39U+ju/fZM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HLIIYcccsghhxxyyCGHHHLIIYcccsghhxxyyCGH/LA7fJJDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455JBDDvlhd/gkhxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYcccsghhxzyw+7wSQ455JBDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455Idv7tCX4cdDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455JAfdodPcsghhxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYcccsghP+wOn+SQQw455JBDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQ37YHT7JIYcccsghhxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYf8sDt8kkMOOeSQQw455JBDDjnkkEMOOeSQQw455JBDDjnkkEMO+WF3+CSHHHLIIYcccsghhxxyyCGHHHLIIYcccsghhxxyyCGHHPLD7vBJDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455JBDDjnkh93hkxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYcccsghhxxyyA/f3KEvw4+HHHLIIYcccsghhxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYcccsghhxxyyCE/7A6f5JBDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw455JBDftgdPskhhxxyyCGHHHLIIYcccsghhxxyyCGHHHLIIYcccsghh/ywO3ySQw455JBDDjnkkEMOOeSQQw455JBDDjnkkEMOOeSQQw754Zs7nNf6/d//N9v/w373t//VP/+t3/r6D/Cf/Yt/+Tu/96+P+gb7t1/vD/7g6/321/vDP/ztP/qjf/fHf/zvv96f/Mnv/Omf/oc/+7P/+Od//rt/8Rf/6S//8vf+L9sMtyhbKNyaAAAAAElFTkSuQmCC

Android端代码如下,

String imageString = Base64.encodeBytes(byteImage);

这是使用的类文件。

http://www.mirthcorp.com/community/issues/secure/attachment/10313/Base64.java

2 个答案:

答案 0 :(得分:2)

可以使用imagecreatefrompng功能检查提供的图像是否损坏。

$src1 = 'F:\Test\0374_1423730003.png';
$src2 = 'F:\Test\5030_1423722865.png'; // broken

$im = @imagecreatefrompng($src1);
if ($im)
    echo 'created img';
else
    echo 'not created, broken image';

答案 1 :(得分:1)

public String BitMapToString(Bitmap bitmap){

        ByteArrayOutputStream baos=new  ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos);

        byte [] b=baos.toByteArray();

        String temp=null;

        try{

            System.gc();

            temp= Base64.encodeToString(b, Base64.DEFAULT);

        }catch(Exception e){

            e.printStackTrace();

        }catch(OutOfMemoryError e){

            baos=new  ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG,50, baos);
            b=baos.toByteArray();

            temp=Base64.encodeToString(b, Base64.DEFAULT);

            Log.e("EWN", "Out of memory error catched");

        }

        return temp;

    }

此方法可帮助您将位图图像转换为Base64编码字符串。