0
我想問如何從Opencart客戶網站(例如檔案圖片)上傳圖片並調整大小?我的代碼可以上傳圖片,但無法調整圖片大小。如果我有文件5MB圖像文件,我想縮小文件大約200KB。我已經嘗試過PHP函數imagejpeg,但它不工作,也不會上傳image.Here是我的代碼:Opencart上傳圖片來自客戶頁面
//Check the post
if((!empty($_FILES)) && ($_FILES['photo']['size'] > 0) && ($_FILES['photo']['error'] == 0)){
//check extension
$fileType = strtolower(pathinfo(basename($this->request->files['photo']['name']), PATHINFO_EXTENSION));
if($fileType != "jpeg" && $fileType != "jpg" && $fileType != "png" && $fileType != "bmp"){
echo "<script>
alert('Extension allowed : jpeg, jpg, png, dan bmp. Your extension: ".$fileType."');
window.location.href='javascript: window.history.go(-1)';
</script>";
die();
}
//check image size
if($this->request->files['photo']['size'] > 5000000){
echo "<script>
alert('File too big, max. 5000000 (5MB). Your file: ".$this->request->files['photo']['size']."');
window.location.href='javascript: window.history.go(-1)';
</script>";
die();
}
$fileName = $this->request->files['photo']['name'];
$newimg = date('YmdHis', time());
$arr = explode(".", $fileName);
$extension = strtolower(array_pop($arr));
$foto = $newimg.".".$extension;
$uploads_dir = 'image/data/profile/';
if (is_uploaded_file($this->request->files['photo']['tmp_name'])) {
//i want to shrink image size before this upload
move_uploaded_file($this->request->files['photo']['tmp_name'], $uploads_dir.$foto);
}
}
else{
echo "<script>
alert('Please choose a photo');
window.location.href='javascript: window.history.go(-1)';
</script>";
die();
$foto = '';
}