Here you can use one method similar to the Photoshop wand tool:
convert original.jpg -alpha set -channel alpha -fuzz 18% \ -fill none -floodfill +0+0 black transparent-border.png
The following are the commands:
convert original.jpg : run Imagemagick with the original image
-alpha set : activate alpha channel
-channel alpha : subsequent statements act on the alpha channel
-fuzz 18% : see -floodfill ...
-fill none : see -floodfill ...
-floodfill +0+0 black : start from the upper left corner ( +0+0 ) and find adjacent pixels within the -fuzz distance of the color black and replace it with -fill
transparent-border.png : output image
Here is the result: 
Unfortunately, as you can see, this method still leaves some dark pixels with the image you provided, because the border is not pure black and a bit mixed with the inner gray border, and the image itself is quite small.
You will get much better results if you have a better source image or a larger one, which you could then reduce in size after the change.
If you are stuck with these small images (or just want to use a different method), I would recommend taking a different route, where you create your own mask shape, which is smaller than the size of the original photo, then add your own gray frame back. I gave an example of this process below.
Possible command for this method:
convert original.jpg mask.png -compose CopyOpacity -composite \ -compose src-over new-border.png -composite clean-result.png
... broken ...
convert original.jpg mask.png : run Imagemagick with the original image and bring mask.png as the second layer (mask.png is a white rounded rectangular shape of the photo on a black background, but the shape is slightly smaller than the original - the result will remove the original gray-black frame).
-compose CopyOpacity -composite : use mask.png to “knock out” the form from the original .png
-compose src-over : reset composite method for easy overlay
new-border.png -composite : overlay gray frame (png - 3 px wide border, 1 pixel on each side of the edge of the mask on a transparent background)
clean-result.png : output image
I created mask.png and new-border.png in Photoshop. You can use Imagemagick vector tools and do it all in one team using only original.jpg, but it will not be easy.
Result: 
In conclusion, I'm not sure if you are using PHP Imagick or Imagemagick from the command line. A few years ago, I tried using Imagick, but quickly became disappointed in the lack of documentation compared to the command line (perhaps this has changed). Instead, I execute commands from PHP (e.g. from exec() or passthru() ). Some or many others will probably tell you that you should never run shell commands from PHP, but while you are avoiding any arguments, I have yet to see a convincing argument against this. Then you will have all the Imagemagick documentation at your disposal ( http://www.imagemagick.org/Usage/ ).
Greetings.