使用php Issue上传之前调整图像大小

时间:2017-08-04 07:44:42

标签: php image-uploading

我遇到问题,因为代码在上传之前没有调整图片大小。也许我错过了什么。

    public function add(){
        // Sanitize POST
        $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

        if($post['submit']){
            if($post['title'] == '' || $post['body'] == '' || $post['link'] == ''){
                Messages::setMsg('Please Fill In All Fields', 'error');
                return;
            }

            $images=$_FILES['upic']['name'];
        $tmp_dir=$_FILES['upic']['tmp_name'];
        $imageSize=$_FILES['upic']['size'];



        $type=$_FILES[‘images’][‘type’];

$type_array = array(‘images/jpg’,’images/jpeg’,’images/gif’,’images/png’,’images/JPG’,’images/JPEG’,’images/GIF’,’images/PNG’);

if(in_array($type,$type_array)){

resizeImage($sourcefile, $max_width=500, $max_height=500, $endfile, $type);

}

function resizeImage($sourcefile,$max_width, $max_height, $endfile, $type){

$width = imagesx( $images );

$height = imagesy( $img );

if ($width > $height) {

if($width < $max_width){

$newwidth = $width;

}else{

$newwidth = $max_width;

$divisor = $width / $newwidth;

$newheight = floor( $height / $divisor);

}

}

else {

if($height < $max_height){

$newheight = $height;

}else{

$newheight =  $max_height;

$divisor = $height / $newheight;

$newwidth = floor( $width / $divisor );

}

}

// Create a new temporary image.

$tmpimg = imagecreatetruecolor( $newwidth, $newheight );



imagealphablending($tmpimg, false);

imagesavealpha($tmpimg, true);

// Copy and resize old image into new image.

imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Save thumbnail into a file.

//compressing the file


// release the memory

imagedestroy($tmpimg);

imagedestroy($img);

}



        $upload_dir='uploads/';
        $imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
        $valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
        $picProfile=rand(1000, 1000000).".".$imgExt;
        move_uploaded_file($tmp_dir, $upload_dir.$picProfile);

            // Insert into MySQL
            $this->query('INSERT INTO shares (title, body, link, user_id, upic) VALUES(:title, :body, :link, :user_id, :upic)');
            $this->bind(':title', $post['title']);
            $this->bind(':body', $post['body']);
            $this->bind(':link', $post['link']);

            $this->bind(':user_id', 1);
            $this->bind(':upic', $upload_dir.$picProfile);
            $this->execute();
            // Verify
            if($this->lastInsertId()){
                // Redirect
                header('Location: '.ROOT_URL.'shares');
            }
        }
        return;
    }
}

1 个答案:

答案 0 :(得分:0)

经过大量的努力,一次又一次地尝试不同的代码,终于找到了一个在一些变化后工作的解决方案。我想分享以指导在上传PHP时寻找调整大小图像的其他人。

$fileName = $_FILES['upic']['name'];
    $tmpName  = $_FILES['upic']['tmp_name'];
    $fileSize = $_FILES['upic']['size']/1024;
    $fileType = $_FILES['upic']['type'];
    $fileExtension = end(explode(".", $fileName));
     if(($fileType == "image/gif" || $fileType == "image/jpeg" || $fileType 
     == "image/pjpeg" || $fileType == "image/png" || $fileType == "image/x-
     png") && $fileSize < 1000000) {
    $newFileName = md5(date('u').rand(0,99)).".".$fileExtension;;
    $imagePath = 'uploads/'.$newFileName;;    
     $result = @move_uploaded_file($tmpName, $imagePath);
     $width = 200;
     $height = 200;
     list($width_orig, $height_orig) = getimagesize($imagePath);
     $ratio_orig = $width_orig/$height_orig;
    if ($width/$height > $ratio_orig) {
    $width = $height*$ratio_orig;
     } else {
     $height = $width/$ratio_orig;}
       $image_p = imagecreatetruecolor($width, $height);
     $image = imagecreatefromjpeg($imagePath);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, 
    $width_orig, $height_orig);
  imagejpeg($image_p, $imagePath, 100);  }
  $this->query('INSERT INTO shares (title, body, link, user_id, upic) 
   VALUES(:title, :body, :link, :user_id, :upic)');
        $this->bind(':title', $post['title']);
        $this->bind(':body', $post['body']);
        $this->bind(':link', $post['link']);

        $this->bind(':user_id', 1);
        $this->bind(':upic', $imagePath);
        $this->execute();