function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
break;
case 'image/png':
$image = imagecreatefrompng($source);
imagepng($image, $destination, $quality);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
imagegif($image, $destination, $quality);
break;
default:
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
}
return $destination;
}
if(isset($_POST['submit'])){
$conn = $pdo->open();
$uploadPath = "draft/";
if(isset($_FILES['photo']['name'][0]) && $_FILES['photo']['size'][0] != 0 && $_FILES['photo']['error'][0] == 0)
{
$filesCount = count($_FILES['photo']['name']);
for($i = 0; $i < $filesCount; $i++) {
$ext = pathinfo($_FILES['photo']['name'][$i], PATHINFO_EXTENSION);
$extensions= array("jpeg","jpg","png");
if(in_array($ext,$extensions)=== false){
$_SESSION['error'] = 'extension not allowed, please choose a .jpg, .jpeg or .png file.';
}
else{
$new_filename = time().$i.'.'.$ext;
// Compress size and upload image
compressImage($_FILES["photo"]["tmp_name"][$i], $uploadPath.$new_filename, 75);
$allfiles[] = $new_filename;
}
}
$uploaded_img = implode(',',$allfiles);
}
else{
$uploaded_img = '';
}
try{
$stmt = $conn->prepare("INSERT INTO marketplace (category_id, post_userid, userid, name, description, slug, price, photo, suburb_province, counter) VALUES (:category, :post_userid, :userid, :name, :description, :name, :price, :photo, :suburb_province, :counter)");
$stmt->execute(['category'=>$category, 'post_userid'=>$post_userid, 'userid'=>$userid, 'name'=>$name, 'description'=>$description, 'slug'=>$slug, 'price'=>$price, 'photo'=>$uploaded_img, 'suburb_province'=>$suburb_province, 'counter'=>0]);
$_SESSION['success'] = 'Product added successfully';
}
catch(PDOException $e){
$_SESSION['error'] = 'Please choose a .jpg, .jpeg or .png file.';
}
$pdo->close();
}
Hi there I have a code below which worked properly without the compressImage
function. After I tried to incorporate the compress function into my upload script I now get zero bytes file uploaded although it still correctly uploads an array of files
The only problem I need help with is that the uploaded file is just a name.extension with zero bytes.
What am I doing wrong?
2
Answers
There might be a problem with how you are trying to store the image. From what I have seen from your code is that you are using the original file name instead of the compressed file name, This can result in zero bytes being uploaded since the original file is not actually being uploaded. Try using this approach.
I modified only slightly and put together a full working example of what you are trying to accomplish – the
compressImage
function works and reduces the quality of suitable images perfectly well. Use the returned value from that function in subsequent operations