$ img in your example is not a file descriptor, it is a GD image resource resource descriptor in memory.
You can use imagecreatefromstring to load an image as follows:
$file=fopen($fileName,"r+b");
flock($file,LOCK_EX);
$imageBinary=stream_get_contents($file);
$img=imagecreatefromstring($imageBinary);
unset($imageBinary);
, :
ob_start();
imagepng($img);
$imageBinary=ob_get_clean();
ftruncate($file,0);
fseek($file,0);
fwrite($file,$imageBinary);
unset($imageBinary);
flock($file,LOCK_UN);
fclose($file);