在上载php时发送已调整大小的图像的路径

在上载php时发送已调整大小的图像的路径,php,path,image-uploading,Php,Path,Image Uploading,我有一个简单的多图像上传脚本,可以调整图像的大小,保持纵横比。调整大小的工作正常。但是,我似乎无法将图像发送到正确的文件夹,即使语法正确。 简而言之,以下是我正在做的事情: 如果文件输入“Image”不为空,则在“./company\u images”内创建一个新文件夹。所创建文件夹的名称是由“$photo\u directory\u name”变量定义的uniqid。在此之后,为每个图像运行resize函数,然后将调整大小的图像放入由“$total_path”变量定义的上载文件夹 if(!em

我有一个简单的多图像上传脚本,可以调整图像的大小,保持纵横比。调整大小的工作正常。但是,我似乎无法将图像发送到正确的文件夹,即使语法正确。

简而言之,以下是我正在做的事情:

如果文件输入“Image”不为空,则在“./company\u images”内创建一个新文件夹。所创建文件夹的名称是由“$photo\u directory\u name”变量定义的uniqid。在此之后,为每个图像运行resize函数,然后将调整大小的图像放入由“$total_path”变量定义的上载文件夹

if(!empty($_FILES["Image"])){

$photo_directory_name = uniqid(rand(100, 1000));    
$photos_path = "company_images/" . $photo_directory_name;       
$directory = "../company_images/";      

if (!file_exists($directory . $photo_directory_name)) {
$upload_dir = mkdir($directory . $photo_directory_name, 0777, TRUE);
}else{
$upload_dir = $directory . $photo_directory_name;
}       


function resize($width, $height){   

  /* Get original image x y*/
  list($w, $h) = getimagesize($_FILES['Image']['tmp_name']);
  /* calculate new image size with ratio */
  $ratio = max($width/$w, $height/$h);
  $h = ceil($height / $ratio);
  $x = ($w - $width / $ratio) / 2;
  $w = ceil($width / $ratio);
  /* new file name */   
  $path =  $width.'x'.$height.'_'.$_FILES['Image']['name']; 

  $total_path = $directory . $photo_directory_name . $path; 


  /* read binary data from image file */
  $imgString = file_get_contents($_FILES['Image']['tmp_name']);
  /* create image from string */
  $image = imagecreatefromstring($imgString);
  $tmp = imagecreatetruecolor($width, $height);
  imagecopyresampled($tmp, $image,
    0, 0,
    $x, 0,
    $width, $height,
    $w, $h);
  /* Save image */
  switch ($_FILES['Image']['type']) {
    case 'image/jpeg':
      imagejpeg($tmp, $total_path, 100);
      break;
    case 'image/png':
      imagepng($tmp, $total_path, 0);
      break;
    case 'image/gif':
      imagegif($tmp, $total_path);
      break;
    default:
      exit;
      break;
  }
  return $total_path;
  /* cleanup memory */
  imagedestroy($image);
  imagedestroy($tmp);
}       


$max_file_size = 1024*1000; // 1mb
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
// thumbnail sizes
$sizes = array(1200 => 1000);

if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['Image'])) {
  if( $_FILES['Image']['size'] < $max_file_size ){

    // get file extension
    $ext = strtolower(pathinfo($_FILES['Image']['name'], PATHINFO_EXTENSION));
    if (in_array($ext, $valid_exts)) {


      /* resize image */
      foreach ($sizes as $w => $h) {
        $files[] = resize($w, $h);
      }

    } else {
     $response["message"] = 'photos_invalid_format';
     $errors++; 
    }
  } else{
    $response["message"] = 'photos_file_too_large';
    $errors++; 
  }
}
echo $total_path;   

}
if(!空($\u文件[“图像”])){
$photo_directory_name=uniqid(兰特(1001000));
$photos\u path=“公司图片/”$photo\u目录\u名称;
$directory=“../company_images/”;
如果(!file_存在($directory.$photo_directory_name)){
$upload_dir=mkdir($directory.$photo_directory_name,0777,TRUE);
}否则{
$upload\u dir=$directory.$photo\u directory\u name;
}       
函数调整大小($width,$height){
/*获取原始图像xy*/
列表($w,$h)=getimagesize($_文件['Image']['tmp_名称]]);
/*使用比率计算新图像大小*/
$ratio=最大值($width/$w,$height/$h);
$h=天花板($h/$R);
$x=($w-$width/$ratio)/2;
$w=ceil($width/$ratio);
/*新文件名*/
$path=$width.x'.$height.''.$\u文件['Image']['name'];
$total_path=$directory.$photo_directory_name.$path;
/*从图像文件中读取二进制数据*/
$imgString=file_get_contents($_FILES['Image']['tmp_name']);
/*从字符串创建图像*/
$image=imagecreatefromstring($imgString);
$tmp=ImageCreateTureColor($width,$height);
imagecopyresampled($tmp,$image,
0, 0,
$x,0,
$width,$height,
$w,$h);
/*保存图像*/
开关($\u文件['Image']['type']){
案例“图像/jpeg”:
imagejpeg($tmp,$total_path,100);
打破
案例“image/png”:
imagepng($tmp,$total_path,0);
打破
案例“image/gif”:
imagegif($tmp,$total_path);
打破
违约:
出口
打破
}
返回$total_path;
/*清理内存*/
图像销毁($图像);
图像处理(tmp);
}       
$max\u file\u size=1024*1000;//1mb
$valid_exts=array('jpeg','jpg','png','gif');
//缩略图大小
$sizes=阵列(1200=>1000);
如果($\u服务器['REQUEST\u METHOD']='POST'和isset($\u文件['Image'])){
如果($\u文件['Image']['size']<$max\u文件大小){
//获取文件扩展名
$ext=strtolower(路径信息($_文件['Image']['name'],路径信息扩展名));
if(在数组中($ext,$valid\u exts)){
/*调整图像大小*/
foreach($w=>h){
$files[]=调整大小($w,$h);
}
}否则{
$response[“message”]=“照片格式无效”;
$errors++;
}
}否则{
$response[“message”]=“照片文件太大”;
$errors++;
}
}
echo$total_路径;
}

非常感谢您的帮助。谢谢

您在resize函数中使用了一些全局变量:

$directory 
$photo_directory_name
function resize($width, $height){   
  global $directory, $photo_directory_name;
  // rest of function
它们需要作为参数传入,或者在该函数中声明为全局变量:

$directory 
$photo_directory_name
function resize($width, $height){   
  global $directory, $photo_directory_name;
  // rest of function

您在resize函数中使用了一些全局变量:

$directory 
$photo_directory_name
function resize($width, $height){   
  global $directory, $photo_directory_name;
  // rest of function
它们需要作为参数传入,或者在该函数中声明为全局变量:

$directory 
$photo_directory_name
function resize($width, $height){   
  global $directory, $photo_directory_name;
  // rest of function

那么大小调整后的图像存储在错误的文件夹中?哪个文件夹?如果未保存任何内容,则可能是权限/访问被拒绝错误。在许多系统上,PHP现在以“无人”的身份运行,而不是apache/ftp/WWW用户。@James是的,这是正确的。它将它们存储在公司图片中,而不是公司图片/$photo\u目录\u名称中。是否将大小调整后的图片存储在错误的文件夹中?哪个文件夹?如果未保存任何内容,则可能是权限/访问被拒绝错误。在许多系统上,PHP现在以“无人”的身份运行,而不是apache/ftp/WWW用户。@James是的,这是正确的。它将它们存储在公司图片中,而不是公司图片/photo\u目录中。不幸的是,这并没有解决问题。它正在创建新文件夹,但是仍然将图像放在company_images中$照片目录名称$路径工作不正常。我的回答给出了一个可能的解释,但可能还有其他解释。尝试调试该行并检查
$total\u path
的值是否符合预期。不幸的是,这并没有解决问题。它正在创建新文件夹,但是仍然将图像放在company_images中$照片目录名称$路径工作不正常。我的回答给出了一个可能的解释,但可能还有其他解释。尝试调试该行并检查
$total\u path
的值是否符合预期。