简单图片,图片在从表单上传时未调整大小

时间:2018-12-05 23:23:00

标签: php function class image-processing image-resizing

大家好,我希望有人可以帮助我,我对网站设计,php之类的东西还很陌生,几个月前我就已经开始工作了,但是我忘记了我所做的事情,我敢肯定,这是一个小错误,但是我一次又一次地阅读了代码后,尝试了一些事情。

这是我用来通过表单解析图像的代码,表单本身正在运行,因此我认为它必须位于我添加的用于处理图像的文件中。

add.php:

public void animFab() {  

    ObjectAnimator scaleX = ObjectAnimator.ofFloat(fab, View.SCALE_X, from, to);  
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(fab, View.SCALE_Y, from, to);  
    AnimatorSet set1 = new AnimatorSet();  

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
        ObjectAnimator translationZ = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, from, to);  
        set1.playTogether(scaleX, scaleY, translationZ);  

    } else {  
        set1.playTogether(scaleX, scaleY);  
    }  
    set1.setDuration(500);  
    set1.setInterpolator(new AccelerateInterpolator());  
    set1.addListener(new AnimatorListenerAdapter() {  
        @Override  
  public void onAnimationEnd(Animator animation) {  

        }  
    });  

    Path path = new Path();  
    path.moveTo(0.0f, 0.0f);  
    path.lineTo(0.5f, 1.3f);  
    path.lineTo(0.75f, 0.8f);  
    path.lineTo(1.0f, 1.0f);  
    Interpolator pathInterpolator = PathInterpolatorCompat.create(path);  

    AnimatorSet set2 = new AnimatorSet();  
    ObjectAnimator scaleXBack = ObjectAnimator.ofFloat(fab, View.SCALE_X, to, from);  
    ObjectAnimator scaleYBack = ObjectAnimator.ofFloat(fab, View.SCALE_Y, to, from);  

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
        ObjectAnimator translationZBack = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from);  
        set2.playTogether(scaleXBack, scaleYBack, translationZBack);  
    } else {  
        set2.playTogether(scaleXBack, scaleYBack);  
    }  
    set2.setDuration(500);  
    set2.setInterpolator(pathInterpolator);  

    final AnimatorSet set = new AnimatorSet();  
    set.playSequentially(set1, set2);  

    set.addListener(new AnimatorListenerAdapter() {  
        @Override  
  public void onAnimationEnd(Animator animation) {  
            super.onAnimationEnd(animation);  
            set.start();  
        }  
    });  
    set.start();  
}

然后我正在使用的SimpleImage 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**

检查add.php源代码时出现的错误:

  $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

0 个答案:

没有答案