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