1

这是我的代码,我已经尝试了几个小时将上传的图像保存在不同大小的服务器文件夹中。但是,图像没有得到保存,也无法找到出错的地方。以前,我曾经使用二进制编写,如下面的第二个代码部分,通过以某种方式从我的移动端获取二进制数据(图片是从相机拍摄并从移动端即时发送的)。但随着这增加数据。我决定甚至使用从我的移动端上传带有多部分类型文件上传的图像文件。

<?php

$imagefile = $_FILES["uploadphoto1"]["name"];
$errors=0;
$uploadPath  = __DIR__."/contents/uploads";
define ("MAX_SIZE","4000");

$imagename = "testingimage";

if($imagefile)
{
    $filename = stripslashes($_FILES['uploadphoto1']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
    {
        echo ' Unknown Image extension ';
        $errors=1;
    }
    else
    {

        $file1 = $_FILES['uploadphoto1']['tmp_name'];

        thumbnail_image($file1, "640", "480",$uploadPath.$imagename);
        thumbnail_image($file1, "480", "340",$uploadPath.$imagename."_preview");

    }
}else{
    echo "No file is inserted>>>>>>>>>";
}

function thumbnail_image($original_file_path, $new_width, $new_height, $save_path="")
{
    $imgInfo = getimagesize($original_file_path);
    $imgExtension = "";

    switch ($imgInfo[2])
    {
        case 1:
            $imgExtension = '.gif';
            break;

        case 2:
            $imgExtension = '.jpg';
            break;

        case 3:
            $imgExtension = '.png';
            break;
    }

    if ($save_path=="") $save_path = "thumbnail".$imgExtension ;

    // Get new dimensions
    list($width, $height) = getimagesize($original_file_path);

    // Resample
    $imageResample = imagecreatetruecolor($new_width, $new_height);

    if ( $imgExtension == ".jpg" )
    {
        $image = imagecreatefromjpeg($original_file_path);
    }
    else if ( $imgExtension == ".gif" )
    {
        $image = imagecreatefromgif($original_file_path);
    }
    else if ( $imgExtension == ".png" )
    {
        $image = imagecreatefrompng($original_file_path);
    }

    imagecopyresampled($imageResample, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    if ( $imgExtension == ".jpg" )
        imagejpeg($imageResample, $save_path.$imgExtension);
    else if ( $imgExtension == ".gif" )
        imagegif($imageResample, $save_path.$imgExtension);
    else if ( $imgExtension == ".png" )
        imagepng($imageResample, $save_path.$imgExtension);

}

function getExtension($str) {

    $i = strrpos($str,".");
    if (!$i) { return ""; }

    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

if(isset($_POST['Submit']) && !$errors)
{
    echo "Image Uploaded Successfully!";

}else{
    echo "Image uploading failed>>>>>>>";
     }

?>

但是当我用来获取二进制数据时,下面的函数使用相同的函数,但是这个代码根本不包含在我现在的代码中:

$file1   = tempnam($uploadPath, 'image1');
$fp1     = fopen($file1, 'wb');
fwrite($fp1, $binary1);// Here I have binary data of image. 
fclose($fp1);

thumbnail_image($file1, "640", "480",$uploadPath.$imagelastid.".jpg");

thumbnail_image($file1, "480", "340",$uploadPath.$imagelastid."_preview.jpg");

如果有人能帮助我解决这个问题并让我走上正轨,我将不胜感激。

我收到此错误

getimagesize(): Read error! in C:\xampp\htdocs\MOBILE_PHP\imagephptest.php on line 48

其中第 48 行是 --> $imgInfo = getimagesize($original_file_path);

有时我会在同一行收到此错误。

getimagesize(): Filename cannot be empty in C:\xampp\htdocs\MOBILE_PHP\imagephptest.php在第 44 行

4

3 回答 3

0
list($width, $height, $type, $attr) = getimagesize($original_file_path);
$widthheight="$width X $height";

这将给出图像的宽度和高度。

于 2013-01-31T09:06:11.463 回答
0

我怀疑问题出在这两行:

$file1 = $_FILES['uploadphoto1']['tmp_name'];
$file1   = tempnam($uploadPath, 'image1');

您获取上传图像的临时名称并将其保存在 $file1 中,然后立即重写该值并创建临时文件。之后,你将它传递给你的函数,当你尝试

getimagesize($original_file_path);

它失败了,因为 $original_file_path 甚至不是图像。

于 2013-01-31T09:11:17.770 回答
0

首先,我建议您在测试 $_FILES["uploadphoto1"]['size']>0 后开始该过程,而不是名称或其他任何内容。另外,我建议您在开始使用它之前在函数中测试 $original_file_path。还可以尝试在函数请求时打印/回显 $original_file_path 以查看您得到的结果。

作为旁注,如果您可以使用 Imagick,请使用它,您不需要那么多代码。

于 2013-01-31T10:03:26.940 回答