I'm presently working in online project..where users upload their images and that images will be first stored in server folder like "uploads" directory...and once the file will moved into that uploads directory..then the path will inserted into the mysql database using mysql query. But the problem is..the images takes longer time to load on webpage...i want to reduce the image size before uploading into database and server..
here is my code
$upload_dir = 'user_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 1000000){
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
And my query looks like..
if(!isset($errMSG))
{
$stmt = $user_home->runQuery("INSERT INTO cam_info (userID,
cam_name,
cam_model,
pixels,
photoshoot,
cam_rent,
cam_img,
mobile,
state,
address,
upd_date,
code )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
$stmt->bindParam(1,$id);
$stmt->bindParam(2,$cam_name);
$stmt->bindParam(3,$modelname);
$stmt->bindParam(4,$pixel);
$stmt->bindParam(5,$photoshoot);
$stmt->bindParam(6,$rentpday);
$stmt->bindParam(7,$userpic);
$stmt->bindParam(8,$usermob);
$stmt->bindParam(9,$state);
$stmt->bindParam(10,$useraddrs);
$stmt->bindParam(11,$upd_date);
$stmt->bindParam(12,$ucode);
if($stmt->execute())
{
$successMSG = "Record saved success";
}
else
{
$errMSG = "error while inserting....";
}
Generally speaking it is not good idea to upload images into the database - the database will grow up very rapidly and may leads to performance and maintain issues in the future.
Use database to keep the metadata about the pictures and keep the images on file system.
Reduce the size is very general question as there are different aspects:
To reduce the bytes you can:
To reduce the dimension: