PHP Issue with using imagefill() for a white background working differently for different images

20 views Asked by At

I have this code, it work on some images and don't work properly on other images and need some code commented to make it work on some other images.

$xOffset = ($width - $image_width) / 2;

// Resample the image 
$image_p = imagecreatetruecolor($new_width, $new_height); 

//change color
$white_background = imagecolorallocate($image_p, 255, 255, 255);
//imagefill($image_p, 0, 0, $white_background); 

//find right side of image
$rightOffset = ($new_width - $xOffset) + 1;  

$image = imagecreatefrompng($image_file); //formula for png file
    
//insert source file into new image
imagecopyresampled($image_p, $image, $xOffset, 0, 0, 0, $new_width, $new_height, $width, $height); 

//fill image right hand side with white
imagefill($image_p, 0, $rightOffset, $white_background);
 
// Output the image to browser 
header('Content-Type: image/jpeg'); 
//imagejpeg($image_p, $save_to, 100); //if want to save file at mentioned path
imagejpeg($image_p, null, 100); //if don't want to save, only display output on page

If I don't comment this line //imagefill($image_p, 0, 0, $white_background);, then I noticed that small png images which are not same as background size, they get right half side in black. So, it needs commented, in that case.

If I comment out //imagefill($image_p, 0, 0, $white_background); then I noticed that in case of full width png images with text, has inner circles of 'B' covered in black. So, this line should not be commented.

Seems like some glitch in code for two different kinds of png images I tested.

0

There are 0 answers