remove black background when resize image in php
/* image resize in php when form submit */
if(isset($_POST['submit']))
{
function resize($width, $height){
include('conn.php');
$root=$_POST['root'];
$category=$_POST['category'];
$sub_category=$_POST['sub-category'];
$product_name=$_POST['product_title'];
$product_specification=mysqli_real_escape_string($con,$_POST['product_description']);
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
$path= '../product_images/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
$product_title= $_POST['product_title'];
$product_description= $_POST['product_description'];
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = dirname($actual_link);
$full_img_url = $actual_link.'/'.$path;
$sql="insert into `products` (id, root, category, sub_category, product_name, specification, image_path) values('', '".$root."', '".$category."', '".$sub_category."','".$product_name."','".$product_specification."','".$full_img_url."')";
$result = mysqli_query($con,$sql);
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
/* Below three lines for remove black background when resize img php */
imagecolortransparent($tmp, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($_FILES['image']['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
// settings
$max_file_size = 1000*600;
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
// thumbnail sizes
$sizes = array(600 => 400);
if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['image'])) {
if( $_FILES['image']['size'] < $max_file_size ){
// get file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
if (in_array($ext, $valid_exts)) {
/* resize image */
foreach ($sizes as $w => $h) {
$files[] = resize($w, $h);
if($files)
{
$message="Project Added successfully";
}
else
{
$message="Project Not Added";
}
}
} else {
$msg = 'Unsupported file';
}
} else{
$msg = 'Please upload image smaller than 200KB';
}
}
}
if(isset($_POST['submit']))
{
function resize($width, $height){
include('conn.php');
$root=$_POST['root'];
$category=$_POST['category'];
$sub_category=$_POST['sub-category'];
$product_name=$_POST['product_title'];
$product_specification=mysqli_real_escape_string($con,$_POST['product_description']);
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
$path= '../product_images/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
$product_title= $_POST['product_title'];
$product_description= $_POST['product_description'];
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = dirname($actual_link);
$full_img_url = $actual_link.'/'.$path;
$sql="insert into `products` (id, root, category, sub_category, product_name, specification, image_path) values('', '".$root."', '".$category."', '".$sub_category."','".$product_name."','".$product_specification."','".$full_img_url."')";
$result = mysqli_query($con,$sql);
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
/* Below three lines for remove black background when resize img php */
imagecolortransparent($tmp, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($_FILES['image']['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
// settings
$max_file_size = 1000*600;
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
// thumbnail sizes
$sizes = array(600 => 400);
if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_FILES['image'])) {
if( $_FILES['image']['size'] < $max_file_size ){
// get file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
if (in_array($ext, $valid_exts)) {
/* resize image */
foreach ($sizes as $w => $h) {
$files[] = resize($w, $h);
if($files)
{
$message="Project Added successfully";
}
else
{
$message="Project Not Added";
}
}
} else {
$msg = 'Unsupported file';
}
} else{
$msg = 'Please upload image smaller than 200KB';
}
}
}
Comments
Post a Comment