2017-08-04 107 views
-2

我有问题,因为代码在上载前没有调整图像大小。也许我错过了一些东西。使用php上传之前调整图像大小问题

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; 
    } 
} 
+0

看起来像(不能完全确定,因为你搞砸代码格式化,硬红 - 请修复),你只移动原始上传文件到您的上传目录。你的resizeImage函数有一个注释://将缩略图保存到一个文件中 - 但这似乎并没有真正的代码实际执行。 – CBroe

+0

因为PHP在服务器端运行,所以它不会调整大小(除非您使用CURL通过php CLI脚本客户端上传文件)。如果你从浏览器上传,你需要js。 –

回答

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();