2013-10-24 32 views
0

怪行我有一个表格,其中有一个输入类型的文件的人的名字是“照片”GD - imagecopyresampled:代码

的目标是调整该用户再进数据库上传照片。

主要错误是,当我点击提交页面显示我吨的怪像这样的代码:

����JFIF��m���b~bs�I$��'$V�ڳ�v,HL���rr0{ �r�I4����nCT�����O���%�vw|��;��[쯧��!VOݓI&Ҽ�M춶��o�Z�ѥ��Vb���������� ۧ��b��zi8Pr�%�9 ��猞3!�Dx�,U�8t�F�cМ�X��lP� 

如果我点击提交我做了以下说明

$fFoto=""; 

if($_FILES['foto']['error'] == UPLOAD_ERR_OK) { 

    $fFile="/upload/inserzionisti/".$id."_".$_FILES["foto"]["name"]; 
    $fFoto=$id."_".$_FILES["foto"]["name"]; 

    $percent = 0.5; 

    list($width,$height) = getimagesize("upload/inserzionisti/".$fFoto.""); 
    $new_width = $width * $percent; 
    $new_height = $height * $percent; 

    $image_p = imagecreatetruecolor($new_width,$new_height); 
    $image = imagecreatefromjpeg("upload/inserzionisti/".$fFoto.""); 

     imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height); 
    imagejpeg($image_p,null,100); 

    move_uploaded_file($_FILES["foto"]["tmp_name"],".".$fFile); 
    $sql = "UPDATE inserzionisti SET FotoUrl ='$fFoto' where Id=$id"; 

待办事项你有什么建议吗?

由于

+0

'imagejpeg'使用空第二参数只是将图像输出到浏览器。这就是你所看到的。 –

+2

在'imagejpeg($ image_p,null,100)'之前''添加'header(“Content-type:image/jpeg”);''你会看到图像。或者改变'imagejpeg()'的第二个参数来保存文件。 – Reeno

+0

确实。这个“奇怪的代码”实际上就是图像本身,只是格式错误;)线索是字母“JFIF”,它是[JPEG文件交换格式]的标头的文件标识符(http:// en。正在输出的wikipedia.org/wiki/JPEG_File_Interchange_Format)文件。 –

回答

0

尝试此代码:

$fFoto=""; 

if($_FILES['foto']['error'] == UPLOAD_ERR_OK) { 
    $fFile="/upload/inserzionisti/".$id."_".$_FILES["foto"]["name"]; 
    $fFoto=$id."_".$_FILES["foto"]["name"]; 

    $percent = 0.5; 

    list($width,$height) = getimagesize("upload/inserzionisti/".$fFoto.""); 
    $new_width = $width * $percent; 
    $new_height = $height * $percent; 

    $image_p = imagecreatetruecolor($new_width,$new_height); 
    // create the new image from the uploaded file 
    $image = imagecreatefromjpeg($_FILES["foto"]["tmp_name"]); 

    imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height); 
    // add the new path as second parameter of imagejpeg() to save the file in the specified location 
    imagejpeg($image_p,$fFile,100); 

    // the following line isn't necessary anymore 
    // move_uploaded_file($_FILES["foto"]["tmp_name"],".".$fFile); 
    $sql = "UPDATE inserzionisti SET FotoUrl ='$fFoto' where Id=$id"; 
+0

谢谢Reeno,我会试试这个! –

+0

Reeno,这是我的一次。新路径是文件夹或文件夹+ foto?在任何情况下,没有错误,但没有保存...谢谢 –

+0

啊,对不起,我想我混淆了变量。 imagejpeg()需要新文件夹+文件名,我认为在你的例子中是$ fFile。我编辑了代码。 – Reeno