duanpacan9388 2018-12-05 23:22
浏览 75

简单图像,图像不从表单上传时调整大小

Good day everyone, I hope someone can help me, I'm very new to website design, php and the like, i had this working before months ago but I've forgotten what i did, I'm fairly certain it's a small error but I've tried a few things after reading through the code again and again.

This is the code i'm using to parse the images through a form, the form itself is working so i figured it had to be in the files i added to process the images.

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**

Then the SimpleImage php i'm using:

  $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;
  }      

  }
  ?>

And the Error I'm getting when inspecting add.php source code:

 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

My Form used to upload images which is then taken to add.php, image names are saved in database and images are stored in a directory, but somehow are skipping the resizing process.

<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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作