I have E-commerce website, In Users system, When user upload/update his/her profile picture then It should be compressed like: If real image size is 1MB then after uploading It should compress and save with 50kb minimum and 200kb maximum, I am doing it like this, Here is my code:
public function updateProfilePic($file, $userid) {
$filename = $file['user_img']['name'];
$filetmp = $file['user_img']['tmp_name'];
$valid_ext = array('png', 'jpeg', 'jpg');
$location = "user/profilepic/" . $filename;
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extensionstr = strtolower($file_extension);
if(!empty($filename)){
if (in_array($file_extensionstr, $valid_ext)) {
//Here i am compressing image
$this->compressImage($filetmp, $location, 9);
return $this->updateProfilePicture($filename, $userid);
} else {
$msg = '<div class="alert alert-danger" role="alert">Invalid file type. You can upload only:-' . implode(', ', $valid_ext) . '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
}
} else {
$msg = '<div class="alert alert-danger" role="alert">Please upload your profile picture.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
}
}
public function updateProfilePicture($filename, $userid){
$update = "UPDATE users SET user_img = '$filename' WHERE user_id = '$userid'";
$result = $this->db->update($update);
if($result){
$msg = '<div class="alert alert-success" role="alert">Profile picture uploaded successfully <a href="profile.php">Go back</a><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
} else {
$msg = '<div class="alert alert-danger" role="alert">Error while uploading profile picture. Pleas try again!<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
return $msg;
}
}
public function compressImage($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg'){
$image = imagecreatefromjpeg($source);
} elseif ($info['mime'] == 'image/png'){
$image = imagecreatefrompng($source);
imagealphablending($image, false);
imagesavealpha($image, true);
}
imagepng($image, $destination, $quality);
}
You can see my compressImage()
function, If i write imagejpeg()
function then It compresses fine but in png (transparent) image, It saves with black background. So, I replaced with imagepng()
function, But It is not compressing the image, It is increasing the size of the image after saving to Database
, like my image size was 2 MB
, When I upload then It saves with size of 11 MB
. I don't know what happened.
To be clear: I just want to compress png and jpg images and transparent images should not save with black background. Please help me