将上传的图像保存在不同大小的服务器文件夹中

时间:2013-01-31 08:53:52

标签: php image upload

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

<?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行是 - &gt; $ imgInfo = getimagesize($ original_file_path);

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

第44行

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

3 个答案:

答案 0 :(得分:0)

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

这将以图像宽度和高度开始。

答案 1 :(得分:0)

我怀疑问题出在以下两行:

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

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

getimagesize($original_file_path);

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

答案 2 :(得分:0)

首先,我建议您在测试$ _FILES [“uploadphoto1”] ['size']&gt; 0后开始此过程,而不是名称或其他任何内容。另外,我建议您在开始使用之前测试函数中的$ original_file_path。还尝试在函数的乞讨时打印/ echo $ original_file_path,看看你到达那里。

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