Простое изображение, изображения не меняются при загрузке из формы - PullRequest
0 голосов
/ 06 декабря 2018

Добрый день всем, я надеюсь, что кто-то может мне помочь, я очень новичок в дизайне сайтов, PHP и тому подобное, у меня это работало до нескольких месяцев назад, но я забыл, что я сделал, я уверен, что этонебольшая ошибка, но я попробовал несколько вещей после прочтения кода снова и снова.

Это код, который я использую для анализа изображений через форму, сама форма работает, поэтому я понял,это должно было быть в файлах, которые я добавил, чтобы обработать изображения.

add.php:

$target1 = "/upload dir/";
$target = $target1 . basename( $_FILES['photo']['name']);

$target21 = "/upload dir/";
$target2 = $target21 . basename( $_FILES['photo2']['name']);

$target31 = "/upload dir/";
$target3 = $target31 . basename( $_FILES['photo3']['name']);

$target41 = "/upload dir/";
$target4 = $target41 . basename( $_FILES['photo4']['name']);

$target51 = "/upload dir/";
$target5 = $target51 . basename( $_FILES['photo5']['name']);

$target61 = "/upload dir/";
$target6 = $target61 . basename( $_FILES['photo6']['name']);

$target71 = "/upload dir/";
$target7 = $target71 . basename( $_FILES['photo7']['name']);

$target81 = "/upload dir/";
$target8 = $target81 . basename( $_FILES['photo8']['name']);

$target91 = "/upload dir/";
$target9 = $target91 . basename( $_FILES['photo9']['name']);

$target101 = "/upload dir/";
$target10 = $target101 . basename( $_FILES['photo10']['name']);
//lots of  other code.**
$con=mysqli_init();
if (!$con)
{
die("mysqli_init failed");
}
if (!mysqli_real_connect($con,$servername,$dbusername,$dbpassword,$dbname))
{
die("Connect Error: " . mysqli_connect_error());
}
mysqli_select_db($con,"$dbname") or die ("could not open 
db".mysqli_connect_error());


$sql="INSERT INTO stock (stocknr, year, brand, model, class, colour, trans, 
fuel, mileage, description, price, pic, pic2, pic3, pic4, pic5, pic6, pic7, 
pic8, pic9, pic10)
VALUES ('$stocknr', '$year', '$brand', '$model', '$class', '$colour', 
'$trans', '$fuel', '$mileage', '$description', '$price', '$pic', '$pic2', 
'$pic3', '$pic4', '$pic5', '$pic6', '$pic7', '$pic8', '$pic9', '$pic10')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_connect_error());
}
else {
echo "1 record added";
}

function store_uploaded_image($html_element_name, $new_img_width, 
$new_img_height) {

$target_dir = "/upload dir/";
$target_file = $target_dir . basename($_FILES[$html_element_name]["name"]);

$image = new SimpleImage();
$image->load($_FILES[$html_element_name]['name']);
$image->resize($new_img_width, $new_img_height);
$image->save($target_file);
return $target_file; //return name of saved file in case you want to store 
it in you database or show confirmation message to user

}

echo "<table>";
store_uploaded_image($pic, 600, 450);
//Writes the photo to the server
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
 {  //Tells you if its all ok
 echo "<tr width='600' height='50' halign='center' valign='center'><td>";
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been 
 uploaded, and your information has been added to the directory";
 echo "</td></tr>";
 }
 else {
 echo "<tr width='600' height='50' halign='center' valign='center'><td>";
 //Gives and error if its not
 echo "Sorry, there was a problem uploading your file.";
 echo "</td></tr>";
 }
 /10 more of these for 10 images**

Тогда php SimpleImage я использую:

  $filename = 'uploaded_image';
  class SimpleImage {

  var $image;
  var $image_type;


  function load($filename) {
  **line 30**-------------------------------------->>
  $image_info = getimagesize($filename);
  $this->image_type = $image_info[2];
  if ($this->image_type == IMAGETYPE_JPEG ) {

     $this->image = imagecreatefromjpeg($filename);
  } elseif( $this->image_type == IMAGETYPE_GIF ) {

     $this->image = imagecreatefromgif($filename);
  } elseif( $this->image_type == IMAGETYPE_PNG ) {

     $this->image = imagecreatefrompng($filename);
  }
  }
  function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, 
  $permissions=null) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image,$filename,$compression);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image,$filename);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image,$filename);
  }
  if( $permissions != null) {

     chmod($filename,$permissions);
  }
 }
 function output($image_type=IMAGETYPE_JPEG) {

  if( $image_type == IMAGETYPE_JPEG ) {
     imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {

     imagegif($this->image);
  } elseif( $image_type == IMAGETYPE_PNG ) {

     imagepng($this->image);
  }
  }
  function getWidth() {

  return imagesx($this->image);
  }
  function getHeight() {

  return imagesy($this->image);
  }
  function resizeToHeight($height) {

  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);
  }

  function resizeToWidth($width) {
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);
  }

  function scale($scale) {
  $width = $this->getWidth() * $scale/100;
  $height = $this->getheight() * $scale/100;
  $this->resize($width,$height);
  }

  function resize($width,$height) {
  **line 100** $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, 
  $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
  }      

  }
  ?>

И ошибка, которую я получаю при проверке исходного кода add.php:

 Warning: getimagesize(): Filename cannot be empty in 
 /directory/SimpleImage.php on line 30
 Fatal error: Uncaught Error: Cannot access empty property in 
 /directory/SimpleImage.php:100
 Stack trace:
 #0 /directory/add.php(171): SimpleImage->resize(450, 300)
 #1 /directory/add.php(178): store_uploaded_image('Test1.jpg', 450, 300)
 #2 {main}
 thrown in /directory/SimpleImage.php on line 100

Моя форма используется для загрузки изображений, которые затем передаются в add.php, имена изображений сохраняются в базе данных, а изображения сохраняютсяв каталоге, но каким-то образом пропускают процесс изменения размера.

<form enctype="multipart/form-data" action="add.php" method="POST">

 Stocknr: <input type="text" name="stocknr">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Alphanumerical, No Spacing, Max 25 Characters**</p>
      <br><br>
 Year: &nbsp;&nbsp;&nbsp;<input type="text" name="year">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Numbers Only, Max 5 Character**</p>
 <br><br>

  Brand: &nbsp;&nbsp;<input type="text" name="brand">
  <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
  size="18px">**Max 20 Characters**</p>
  <br><br>
  Model: &nbsp;&nbsp;<input type="text" name="model">
  <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
  size="18px">**Max 20 Characters**</p>         
  <br><br>

  <?php
 $servername = "servername";
 $dbusername = "username";
 $dbpassword = "password";
 $dbname = "dbname";

 $con=mysqli_init();
 if (!$con)
 {
 die("mysqli_init failed");
 }
 if (!mysqli_real_connect($con,$servername,$dbusername,$dbpassword,$dbname))
 {
 die("Connect Error: " . mysqli_connect_error());
  }

 echo "<font face='krona-one' size='3'>Vehicle Type:";
 echo "<select name='class' 'class' class='class'><option value='all'><font 
 face='krona-one' size='3'>Select</option>";



  mysqli_select_db($con,"$dbname");

 $sql = "SELECT type FROM class";
 $result = mysqli_query($con,$sql);

 while ($row = mysqli_fetch_array($result)) {
 echo "<option value='" . $row['type'] ."'>" . $row['type'] ."</option>";
 }
 echo "</select>";
  ?> 
 <br><br>
 Mileage: <input type="text" name = "mileage">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Numbers, No Spacing, Max 25 Characters**</p>          
 <br><br>
 Colour: &nbsp;&nbsp;&nbsp;<input type="text" name = "colour">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Max 20 Characters, No Spacing**</p>
 <br><br>
 Transmission: &nbsp;&nbsp;&nbsp;<input type="text" name = "trans">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Max 20 Characters, No Spacing**</p>
<br><br>
Fuel: &nbsp;&nbsp;&nbsp;<input type="text" name = "fuel">
<p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
size="18px">**Max 20 Characters**</p>
<br><br>
Description: <br><textarea name="description" rows="4" cols="40"> 
</textarea><br><br>
Price: &nbsp;&nbsp;&nbsp;<input type="text" name = "price">
<p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
size="18px">**Numbers Only, No Spacing, Leave out "R"**</p>
<br><br>
Photo: &nbsp;&nbsp;<input type="file" name="photo"><br>
Photo2: <input type="file" name="photo2"><br>
Photo3: <input type="file" name="photo3"><br>
Photo4: <input type="file" name="photo4"><br>
Photo5: <input type="file" name="photo5"><br>
Photo6: <input type="file" name="photo6"><br>
Photo7: <input type="file" name="photo7"><br>
Photo8: <input type="file" name="photo8"><br>
Photo9: <input type="file" name="photo9"><br>
Photo10: <input type="file" name="photo10"><br>
<br>
<input type="submit" value="Add" >
</form>
...