覆盖php中的输入


Overwrite input in php

我想上传一个图像并在php中调整它的大小。我使用这个输入来上传:

 <td><form action="uploadpic.php" method="POST" enctype="multipart/form-data" />
        <input type="file" name="user_image" id="user_image" />
        <input name = "button" type = "button" id = "button" 
                          value = "Küldés" onclick="subm(this.form,'_blank');">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
 function subm(f,newtarget){
f.submit();
}
</script>

我在uploadpic。php文件中所做的是:

$target_path = "images/clients/";
$target_path = $target_path . basename( $_FILES['user_image']['name']); 
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
echo "original width ". $width." height ". $height;
$r = $width / $height;
if ($crop) {
    if ($width > $height) {
        $width = ceil($width-($width*abs($r-$w/$h)));
    } else {
        $height = ceil($height-($height*abs($r-$w/$h)));
    }
    $newwidth = $w;
    $newheight = $h;
} else {
    if ($w/$h > $r) {
        $newwidth = $h*$r;
        $newheight = $h;
    } else {
        $newheight = $w/$r;
        $newwidth = $w;
    }
}
 echo "new width ". $newwidth." height ". $newheight;
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['user_image']['name']). 
"Image has been uploaded";
$_FILES['user_image'] = resize_image($target_path, 50, 50);
echo "The file ". basename( $_FILES['user_image']['name']). 
" Image has been resized";
if(move_uploaded_file($_FILES['user_image']['name'], $target_path)) {
echo "The file ".  basename( $_FILES['user_image']['name']). 
"   Resized image has been uploaded";
}else{
echo "  Resized not uploaded because of error #".$_FILES['user_image']['error'];
}
$image_name = 'alma';
$_SESSION['image_name']= 'alma';
} else{
echo "Not uploaded because of error #".$_FILES['user_image']['error'];
}

这个工作直到我试图覆盖user_image输入。我要做的是上传图像调整大小然后上传调整大小的图像。正如你所看到的,我试图用这一行覆盖user_image输入:$_FILES['user_image'] = resize_image($target_path, 50, 50);

它不工作,所以我想问我怎么能覆盖它并上传到服务器?

我推荐使用这个很棒的php库。

这是我的代码中可能对你有所帮助的相关部分:

<?php 
.
.
.
.
require_once('ResizeImage.php');
if(isset($_FILES['photoFile']) && $_FILES['photoFile']['error'] == 0){
 $extension = pathinfo($_FILES['photoFile']['name'], PATHINFO_EXTENSION);
 if(!in_array(strtolower($extension), $allowed)){
  $response = json_encode(array("error" => "true","message" => "The image has the wrong extention"));
  echo $response;
  exit;
}
/*generate unique filename*/
$t=time();
$x=$_FILES['photoFile']['name'];
$name=md5($x.$t);
/*Moves the uploaded file*/
if(move_uploaded_file($_FILES['photoFile']['tmp_name'], '../uploads/original/'.$name.'.'.$extension)){
  $firstimage = '../uploads/original/'.$name.'.'.$extension;
  $secondimage = '../uploads/original/'.$name.'.'.$extension;
  $small2 = compress($firstimage, $firstimage, 45);
  $resize = new ResizeImage($small2);
  unset($small2);
  $resize->resizeTo(250, 250, 'maxWidth');
  $resize->saveImage('../uploads/thumb/'.$name.'.jpg');
  unset($resize);
        //Compress & resize image large
  $large = compress($secondimage, $secondimage, 45);
  $resize2 = new ResizeImage($large);
  unset($large);
  $resize2->resizeTo(560, 560, 'maxWidth');
  $resize2->saveImage('../uploads/large/'.$name.'.jpg');
  unset($resize2);
  /*file location for database*/
  $fileThumb = "uploads/thumb/".$name.".jpg";
  $fileLarge = "uploads/large/".$name.".jpg";
  $response = saveDatabase($nombre,$apellido,$email,$pais,$ciudad,$mensaje,$fileThumb,$fileLarge,$ip);
  if ($response == 1) {
    $response = json_encode(array("error" => "false","message" => "Thanks"));
    echo $response;
  }else{
   $response = json_encode(array("error" => "true","message" => "unknown error "));
   echo $response;
 }
}
}
?>

首先,我们验证$_FILES['photoFile']确实存在并且没有错误,然后我们将它保存到一个临时操作中,调整它的大小,压缩它并将它保存到一个新的位置。

Home it help