Use GD library to create transparent images when multiple images merge inner image edge blur solution
Recently, I developed a project whose function is image and text to generate a simple logo, which is relatively simple. I realized it by using THE GD library of PHP for image processing. During the development, there was a problem that the edge of the image would be blurred with a black line when the background was transparent
The code is as follows:
// Create an image instance
$filename = $choose_image['url'];
$info = getimagesize($filename);
$width = $info[0]; $height = $info[1];
$new_height = (200/$info[0])*$info[1]; // Get the suffix of the image
$type = image_type_to_extension($info[2], false); // Splice image resource handle function
$func = 'imagecreatefrom'.$type; // Create an image resource handle
$image = $func($filename);
$im = imagecreatetruecolor(300, 300);
Imagecopyresampled ($im, $image, 50, 0,0,0,200, $new_height, $width, $height); $red = imagecolorallocate ($im, 255255255);
Imagefill ($im, 0, 0, $red);
imagecolortransparent($im,$red);
// Set the file path
$ml = 'uploads/picture'.'/'.date('Ymd', time());
// Directory name
$url =$ml.'/'.time();
// Storage path
imagepng($im, $url . image_type_to_extension(IMAGETYPE_PNG),9);
The solution is to create a new canvas, put the logo image on the new canvas and merge the two canvasses with imagecopyresampled(), the effect is as follows
The code is as follows:
// Create an image instance
$filename = $choose_image['url'];
$info = getimagesize($filename);
$width = $info[0];
$height = $info[1];
$info_im = imagecreatetruecolor($width, $height);
$info_color = imagecolorallocate($info_im, 255, 255, 255);
// White background
imagefill($info_im, 0, 0, $info_color);
$new_height = (200/$info[0])*$info[1];
// Get the suffix of the image
$type = image_type_to_extension($info[2], false);
// Splice image resource handle function
$func = 'imagecreatefrom'.$type;
// Create an image resource handle
$image = $func($filename);
Imagecopyresampled ($info_im, $image, 0, 0, 0, $width, $height, $width, $height);
$im = imagecreatetruecolor(300, 300);
Imagecopyresampled ($im, $info_im, 50, 0,0,0,200, $new_height, $width, $height); $red = imagecolorallocate ($im, 255255255);
Imagefill ($im, 0, 0, $red);
imagecolortransparent($im,$red);
This is the first time I sent the article content is more basic, but this problem should be few people will encounter more partial, here to record the hope to help others