-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;
}
}
看起來像(不能完全確定,因爲你搞砸代碼格式化,硬紅 - 請修復),你只移動原始上傳文件到您的上傳目錄。你的resizeImage函數有一個註釋://將縮略圖保存到一個文件中 - 但這似乎並沒有真正的代碼實際執行。 – CBroe
因爲PHP在服務器端運行,所以它不會調整大小(除非您使用CURL通過php CLI腳本客戶端上傳文件)。如果你從瀏覽器上傳,你需要js。 –