【发布时间】:2013-01-15 07:35:43
【问题描述】:
这是我的代码,我已经尝试了几个小时以将上传的图像保存在不同大小的服务器文件夹中。但是,图像没有得到保存,也无法找到出错的地方。以前,我曾经在下面的第二个代码部分中使用二进制编写,方法是从我的移动端以某种方式获取二进制数据(图片是从相机拍摄并从移动端即时发送的)。但随着这增加数据。我决定甚至使用从我的移动端上传带有多部分类型文件上传的图像文件。
<?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 行
【问题讨论】:
-
您是否收到任何错误消息?
-
它在哪里停止按预期工作?
-
@KennyPowers 请看我的编辑。
-
@VladPreda 编辑了我的问题
-
我真的找不到任何关于它的信息,但由于 $_FILES['uploadphoto1']['tmp_name'] 是文件的临时副本,也许你无论如何都无法操作它想?尝试使用 move_uploaded_file() 将其保存在某处,然后将保存文件的路径传递给您的函数,完成后,只需删除移动的文件。