We want all the uploaded images to start from the same position so we would like to crop/remove the transparent backgrounds from the images. Here are 2 examples of images uploaded:
As you can see the "A" letter is in the middle with a transparent background while the "F" letter starts from the left with a transparent background.
Here is the code to remove the transparent background while uploading the images:
$extension = strtolower(pathinfo($_FILES['avatar']['name'])['extension']);
if($extension == 'png'){
$image = $_FILES['avatar']['tmp_name'];
$filePath = "images/image.png";
$im = imagecreatefrompng($image);
$cropped = imagecropauto($im, IMG_CROP_DEFAULT);
if ($cropped !== false) {
imagedestroy($im);
$im = $cropped;
}
imagepng($im, $filePath);
imagedestroy($im);
}
The script saves images like this:
I tried to use the following functions before cropping:
imagealphablending($im, false);
imagesavealpha($im, true);
$cropped = imagecropauto($im, IMG_CROP_DEFAULT);
But didn't work.



This is a known GD bug for images with alpha channel, afaik. If you don't mind to replace the transparent background with a white one, try this: